Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
2
1 Thêm mới dữ liệu trong MVC
Figure 1. Sửa đổi controller SanPhamController.cs
\DataClasses.
.
Models\DataClasses.cs
using System;
using System.Collections.Generic;
using System.Linq;
namespace BanHang.Models
{
partial class DataClassesDataContext
{
public List<LoaiSanPham> LayCacLoaiSanPham()
{
return LoaiSanPhams.ToList();
}
public List<SanPham> LaySanPhamTuLoaiSanPham(int id)
{
return SanPhams.Where(l => l.LoaiSanPham == id).ToList();
}
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
3
public SanPham LaySanPhamTuID(int id)
{
return SanPhams.Single(s => s.Id == id);
}
public void ThemMoiSanPham(SanPham sp)
{
SanPhams.InsertOnSubmit(sp);
}
}
}
ThemMoiSanPham và
.
SanPhamController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using BanHang.Models;
namespace BanHang.Controllers
{
public class SanPhamController : Controller
{
DataClassesDataContext data = new DataClassesDataContext();
public ActionResult Index()
{
// Add action logic here
ViewData["Title"] = "Sn phẩm";
return RedirectToAction("DanhMucLoaiSanPham");
}
public ActionResult DanhMucLoaiSanPham()
{
// Code cua ban o day
ViewData["Title"] = "Danh mục loại sản phẩm";
List<LoaiSanPham> lsp = data.LoaiSanPhams.ToList();
return View("DanhMucLoaiSanPham", lsp);
}
public ActionResult DanhSachSanPham(int id)
{
ViewData["Title"] = "Danh sách sản phẩm trong loại sản phẩm";
List<SanPham> sp = data.LaySanPhamTuLoaiSanPham(id);
return View("DanhSachSanPham", sp);
//DuLieuDanhSachSanPham sp = new DuLieuDanhSachSanPham();
//ViewData.TenLoaiSanPham = loaisanpham;
//ViewData.SanPham = data.LaySanPhamTuLoaiSanPham(loaisanpham);
//return View("DanhSachSanPham", ViewData);
}
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
4
public ActionResult ChiTietSanPham(int id)
{
ViewData["Title"] = "Chi tit sn phm";
SanPham ctsp = data.LaySanPhamTuID(id);
return View("ChiTietSanPham", ctsp);
}
public ActionResult ThemMoiSanPham()
{
ViewData["Title"] = "Thêm mới sn phm";
//List<LoaiSanPham> dulieu = data.LayCacLoaiSanPham();
var loaiSanpham = from c in data.LoaiSanPhams select c;
ViewData["lsp"] = new SelectList(loaiSanpham, "Id", "TenLoaiSanPham");
return View("ThemMoiSanPham", ViewData["lsp"]);
}
public ActionResult Create(string TenSanPham, float DonGia, int SoLuong,
int loaiSanPham)
{
SanPham sp = new SanPham();
sp.TenSanPham = TenSanPham;
sp.DonGia = DonGia;
sp.SoLuong = SoLuong;
sp.LoaiSanPham = loaiSanPham;
data.ThemMoiSanPham(sp);
data.SubmitChanges();
return RedirectToAction("DanhMucLoaiSanPham");
}
}
}
.
Views\SanPham\ThemMoiSanPham.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
AutoEventWireup="true" CodeBehind="ThemMoiSanPham.aspx.cs"
Inherits="BanHang.Views.SanPham.ThemMoiSanPham" %>
<asp:Content ID="viewThemMoiSanPham" ContentPlaceHolderID="MainContent"
runat="server">
<form action="Create" method="post">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Tên sn phm:</td>
<td><input type="text" id="TenSanPham" name="TenSanPham" /></td>
</tr>
<tr>
<td>Đơn giá:</td>
<td><input type="text" id="DonGia" name="DonGia" /></td>
</tr>
<tr>
<td>Số lượng:</td>
<td><input type="text" id="SoLuong" name="SoLuong" /></td>
</tr>
<tr>
<td>Loại sn phm:</td>
<td>
<% <input type="text" id="LoaiSanPham" name="LoaiSanPham" />
%>
<%=
Html.DropDownList("loaiSanPham",(SelectList)ViewData["lsp"]) %>
</td>
</tr>
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
5
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Thêm mới sn phm"
/></td>
</tr>
</table>
</form>
</asp:Content>
(figure 2)
Figure 2. Thư
̣
c hiê
̣
n thêm mơ
́
i sa
̉
n phâ
̉
m trong MVC
2
Views\SanPham\DanhSachSanPham.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
AutoEventWireup="true" CodeBehind="DanhSachSanPham.aspx.cs"
Inherits="BanHang.Views.SanPham.DanhSachSanPham" %>
<asp:Content ID="viewDanhSachSanPham" ContentPlaceHolderID="MainContent"
runat="server">
<h1>Đây là danh sách sản phẩm có trong chuyên mục</h1>
<p>
<ul>
<% foreach (var sp in ViewData.Model)
{ %>
<li>
<%= Html.ActionLink(sp.TenSanPham , "ChiTietSanPham/" + sp.Id,
"SanPham") %>
(<%= Html.ActionLink("Edit" , "CapNhatSanPham/" + sp.Id, "SanPham")
%>)
</li>
<% } %>
</ul>
</p>
<p>
<form action=" /ThemMoiSanPham" method="post">
<input type="submit" value="Thêm mới một sn phm" />
</form>
</p>
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
6
</asp:Content>
. (figure 3)
Figure 3. Danh sa
́
ch sa
̉
n phâ
̉
m đa
̃
đươ
̣
c thay đô
̉
i.
Thêm 2
\SanPhamController.cs
Controllers\SanPhamController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using BanHang.Models;
namespace BanHang.Controllers
{
public class SanPhamController : Controller
{
DataClassesDataContext data = new DataClassesDataContext();
public ActionResult Index()
{
// Add action logic here
ViewData["Title"] = "Sn phẩm";
return RedirectToAction("DanhMucLoaiSanPham");
}
public ActionResult DanhMucLoaiSanPham()
{
// Code cua ban o day
ViewData["Title"] = "Danh mục loại sản phẩm";
List<LoaiSanPham> lsp = data.LoaiSanPhams.ToList();
return View("DanhMucLoaiSanPham", lsp);
}
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
7
public ActionResult DanhSachSanPham(int id)
{
ViewData["Title"] = "Danh sách sản phẩm trong loại sản phẩm";
List<SanPham> sp = data.LaySanPhamTuLoaiSanPham(id);
return View("DanhSachSanPham", sp);
//DuLieuDanhSachSanPham sp = new DuLieuDanhSachSanPham();
//ViewData.TenLoaiSanPham = loaisanpham;
//ViewData.SanPham = data.LaySanPhamTuLoaiSanPham(loaisanpham);
//return View("DanhSachSanPham", ViewData);
}
public ActionResult ChiTietSanPham(int id)
{
ViewData["Title"] = "Chi tit sn phm";
SanPham ctsp = data.LaySanPhamTuID(id);
return View("ChiTietSanPham", ctsp);
}
public ActionResult ThemMoiSanPham()
{
ViewData["Title"] = "Thêm mới sn phm";
//List<LoaiSanPham> dulieu = data.LayCacLoaiSanPham();
var loaiSanpham = from c in data.LoaiSanPhams select c;
ViewData["lsp"] = new SelectList(loaiSanpham, "Id", "TenLoaiSanPham");
return View("ThemMoiSanPham", ViewData["lsp"]);
}
public ActionResult Create(string TenSanPham, float DonGia, int SoLuong,
int loaiSanPham)
{
SanPham sp = new SanPham();
sp.TenSanPham = TenSanPham;
sp.DonGia = DonGia;
sp.SoLuong = SoLuong;
sp.LoaiSanPham = loaiSanPham;
data.ThemMoiSanPham(sp);
data.SubmitChanges();
return RedirectToAction("DanhMucLoaiSanPham");
}
public ActionResult CapNhatSanPham(int id)
{
ViewData["Title"] = "Cp nht sn phm";
var spToEdit = (from sp in data.SanPhams
where sp.Id = id
select sp).First();
ViewData.Model = spToEdit;
return View();
}
public ActionResult Update()
{
return RedirectToAction("DanhMucLoaiSanPham");
}
}
}
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
8
\SanPhamController.
CapNhatSanPham,
(figure 4)
Figure 4. Thêm mơ
́
i view cho phương thư
́
c CapNhatSanPham
:
Created a strongly typed view,
View content Edit, View data class BanHang.Models.SanPham
\SanPham\CapNhatSanPham.aspx (Figure 5)
Figure 5. To view CapNhatSanPham tư
̀
controller CapNhatSanPham
\SanPham\
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
9
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<BanHang.Models.SanPham>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h2>CapNhatSanPham</h2>
<%= Html.ValidationSummary() %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Id">Id:</label>
<%= Html.TextBox("Id") %>
<%= Html.ValidationMessage("Id", "*") %>
</p>
<p>
<label for="TenSanPham">TenSanPham:</label>
<%= Html.TextBox("TenSanPham") %>
<%= Html.ValidationMessage("TenSanPham", "*") %>
</p>
<p>
<label for="DonGia">DonGia:</label>
<%= Html.TextBox("DonGia") %>
<%= Html.ValidationMessage("DonGia", "*") %>
</p>
<p>
<label for="SoLuong">SoLuong:</label>
<%= Html.TextBox("SoLuong") %>
<%= Html.ValidationMessage("SoLuong", "*") %>
</p>
<p>
<label for="LoaiSanPham">LoaiSanPham:</label>
<%= Html.TextBox("LoaiSanPham") %>
<%= Html.ValidationMessage("LoaiSanPham", "*") %>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
3
.NET MVC
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
10
Figure 6. Ứng dụng test được to
3.1 To m test.
Controllers\SanPhamController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using BanHang.Models;
namespace BanHang.Controllers
{
public class SanPhamController : Controller
{
DataClassesDataContext data = new DataClassesDataContext();
public ActionResult Index()
{
// Add action logic here
ViewData["Title"] = "Sn phẩm";
return RedirectToAction("DanhMucLoaiSanPham");
}
public ActionResult DanhMucLoaiSanPham()
{
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
11
// Code cua ban o day
ViewData["Title"] = "Danh mục loại sản phẩm";
List<LoaiSanPham> lsp = data.LoaiSanPhams.ToList();
return View("DanhMucLoaiSanPham", lsp);
}
public ActionResult DanhSachSanPham(int id)
{
ViewData["Title"] = "Danh sách sản phẩm trong loại sản phẩm";
List<SanPham> sp = data.LaySanPhamTuLoaiSanPham(id);
return View("DanhSachSanPham", sp);
//DuLieuDanhSachSanPham sp = new DuLieuDanhSachSanPham();
//ViewData.TenLoaiSanPham = loaisanpham;
//ViewData.SanPham = data.LaySanPhamTuLoaiSanPham(loaisanpham);
//return View("DanhSachSanPham", ViewData);
}
public ActionResult ChiTietSanPham(int id)
{
ViewData["Title"] = "Chi tiết sản phẩm";
SanPham ctsp = data.LaySanPhamTuID(id);
return View("ChiTietSanPham", ctsp);
}
public ActionResult ThemMoiSanPham()
{
ViewData["Title"] = "Thêm mới sn phm";
//List<LoaiSanPham> dulieu = data.LayCacLoaiSanPham();
var loaiSanpham = from c in data.LoaiSanPhams select c;
ViewData["lsp"] = new SelectList(loaiSanpham, "Id", "TenLoaiSanPham");
return View("ThemMoiSanPham", ViewData["lsp"]);
}
public ActionResult Create(string TenSanPham, float DonGia, int SoLuong,
int loaiSanPham)
{
SanPham sp = new SanPham();
sp.TenSanPham = TenSanPham;
sp.DonGia = DonGia;
sp.SoLuong = SoLuong;
sp.LoaiSanPham = loaiSanPham;
data.ThemMoiSanPham(sp);
data.SubmitChanges();
return RedirectToAction("DanhMucLoaiSanPham");
}
public ActionResult CapNhatSanPham(int id)
{
ViewData["Title"] = "Cập nhất sản phẩm";
var spToEdit = (from sp in data.SanPhams
where sp.Id == id
select sp).First();
ViewData.Model = spToEdit;
return View();
}
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
12
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Update()
{
//UpdateModel(sp,FormCollection.KeysCollection);
return RedirectToAction("DanhMucLoaiSanPham");
}
}
}
\S trên, o SanPhamControllerTest.cs
Unit Test (figure 7)
Figure 7. Xây dựng test cho controller
Figure 8. Check chọn phương thức cần test
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
13
BanHang.Tests\Controllers\SanPhamControllerTest.cs
using BanHang.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System.Web.Mvc;
namespace BanHang.Tests
{
/// <summary>
///This is a test class for SanPhamControllerTest and is intended
///to contain all SanPhamControllerTest Unit Tests
///</summary>
[TestClass()]
public class SanPhamControllerTest
{
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the
class
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//Use ClassCleanup to run code after all tests in a class have run
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Use TestCleanup to run code after each test has run
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
14
#endregion
/// <summary>
///A test for ChiTietSanPham
///</summary>
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("D:\\@Projects\\@Test\\HiTest\\BanHang\\BanHang",
"/")]
[UrlToTest("http://localhost:2430/")]
public void ChiTietSanPhamTest()
{
SanPhamController target = new SanPhamController(); // TODO: Initialize
to an appropriate value
int id = 0; // TODO: Initialize to an appropriate value
ActionResult expected = null; // TODO: Initialize to an appropriate
value
ActionResult actual;
actual = target.ChiTietSanPham(id);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
}
}
3.2 Test mc tr v t mt controller.
.
BanHang.Tests\Controllers\SanPhamControllerTest.cs
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("D:\\@Projects\\@Test\\HiTest\\BanHang\\BanHang",
"/")]
[UrlToTest("http://localhost:2430/")]
public void DetailViewTest()
{
SanPhamController sp = new SanPhamController();
var result = sp.ChiTietSanPham(8) as ViewResult;
Assert.AreEqual("ChiTietSanPham", result.ViewName);
}
. Run Selection
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
15
Figure 9. Thực hiện các phương thức test
Figure 10. Kết quả thực hiện test DetailViewTest
3.3 Test mt ViewData c tr v t mt controller
BanHang.Tests\Controllers\SanPhamControllerTest.cs
[TestMethod]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("D:\\@Projects\\@Test\\HiTest\\BanHang\\BanHang",
"/")]
[UrlToTest("http://localhost:2430/")]
public void TestViewData()
Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC
16
{
//
// TODO: Add test logic here
//
var controller = new BanHang.Controllers.SanPhamController();
var result = controller.ChiTietSanPham(8) as ViewResult;
var sp = (SanPham)result.ViewData.Model;
Assert.AreEqual("Yamaha", sp.TenSanPham);
}
3.4 Test mc tr v t mt controller
BanHang.Tests\Controllers\SanPhamControllerTest.cs
[TestMethod]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("D:\\@Projects\\@Test\\HiTest\\BanHang\\BanHang",
"/")]
[UrlToTest("http://localhost:2430/")]
public void TestResultAction()
{
//
// TODO: Add test logic here
//
var controller = new BanHang.Controllers.SanPhamController();
var result = (RedirectToRouteResult)controller.ChiTietSanPham(-1);
Assert.AreEqual("Index", result.Values["action"]);
}
4
/>scenarios.aspx