Hướng dẫn lập trình asp.net 3.5 microsoft - bài 5: content mvc asp.net potx

16 250 0
Hướng dẫn lập trình asp.net 3.5 microsoft - bài 5: content mvc asp.net potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

 5  Table of Contents 1 Thêm mới dữ liệu trong MVC 2 2 S      5 3 Xây d    .NET MVC 9 3.1  10 3.2  14 3.3  15 3.4  16 4  16 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"] = "Sn 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 tit sn phm"; SanPham ctsp = data.LaySanPhamTuID(id); return View("ChiTietSanPham", ctsp); } public ActionResult ThemMoiSanPham() { ViewData["Title"] = "Thêm mới sn phm"; //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 sn phm:</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 sn phm:</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">&nbsp;</td> </tr> <tr> <td colspan="2"><input type="submit" value="Thêm mới sn phm" /></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 sn phm" /> </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"] = "Sn 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 tit sn phm"; SanPham ctsp = data.LaySanPhamTuID(id); return View("ChiTietSanPham", ctsp); } public ActionResult ThemMoiSanPham() { ViewData["Title"] = "Thêm mới sn phm"; //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(); } 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. To 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 to 3.1 To 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"] = "Sn phẩm"; return RedirectToAction("DanhMucLoaiSanPham"); } public ActionResult DanhMucLoaiSanPham() { [...]... (RedirectToRouteResult)controller.ChiTietSanPham (-1 ); Assert.AreEqual("Index", result.Values["action"]); } 4 Tài liệu tham khảo http:/ /asp.net/ mvc http://weblogs .asp.net/ scottgu/archive/2007/12/09/asp-net -mvc- framework-part-4-handling-form-edit-and-postscenarios.aspx Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC 16 ... cần được test (figure 8) 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 12 BanHang.Tests\Controllers\SanPhamControllerTest.cs using using using using BanHang.Controllers; Microsoft. VisualStudio.TestTools.UnitTesting; Microsoft. VisualStudio.TestTools.UnitTesting.Web; System.Web .Mvc; namespace BanHang.Tests { /// ///This is a test class... chọn một phương thức test  Run Selection Microsoft Vietnam – DPE Team |Bài số 5: Chỉnh sửa dữ liệu với MVC 14 Figure 9 Thực hiện các phương thức test Kết quả thực hiện test (figure 10) Figure 10 Kết quả thực hiện test DetailViewTest 3.3 Test một ViewData được trả về từ một controller BanHang.Tests\Controllers\SanPhamControllerTest.cs [TestMethod] [HostType( "ASP.NET" )] [AspNetDevelopmentServerHost("D:\\@Projects\\@Test\\HiTest\\BanHang\\BanHang",... //} // //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 13 #endregion /// ///A test for ChiTietSanPham /// [TestMethod()] [HostType( "ASP.NET" )] [AspNetDevelopmentServerHost("D:\\@Projects\\@Test\\HiTest\\BanHang\\BanHang", "/")] [UrlToTest("http://localhost:2430/")]... [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 15 { // // TODO: Add test logic here // var controller = new BanHang.Controllers.SanPhamController(); var result = controller.ChiTietSanPham(8) as ViewResult; var sp... ViewData["Title"] = "Cậ nhấ sả phẩ "; p t n 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 11 [AcceptVerbs(HttpVerbs.Post)] public ActionResult Update() { //UpdateModel(sp,FormCollection.KeysCollection); return RedirectToAction("DanhMucLoaiSanPham"); } } }... 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"]);... controller Kiểm tra xem view được trả về có đúng không và khi nào thì nó được gọi đến, phương thức nào của view được trả về 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(); . (RedirectToRouteResult)controller.ChiTietSanPham (-1 ); Assert.AreEqual("Index", result.Values["action"]); } 4  http:/ /asp. net/ mvc http://weblogs .asp. net/ scottgu/archive/2007/12/09 /asp- net- mvc- framework-part-4-handling-form-edit-and-post- scenarios.aspx. http://weblogs .asp. net/ scottgu/archive/2007/12/09 /asp- net- mvc- framework-part-4-handling-form-edit-and-post- scenarios.aspx .  5  Table of Contents 1 Thêm mới dữ liệu trong MVC 2 2 S      5 3 Xây d     .NET MVC 9 3. 1 

Ngày đăng: 12/08/2014, 19:21

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan