You are on page 1of 24

Mvc V Asp.

net
I//Tm Hiu MVC
Mvc l g ? Ta s hiu MVC l vit tt ca 3 t : + M => Models + V => Views + C =>Controllers Ngy xa cc bn lp trnh mt trang jsp, php hay asp, cc bn thng an xen cc m html cng vi cc m jsp, php, hay asp. Do vy, c nhng kh khn sau c th gp phi: Ngi thit k giao din cng cn phi bit cc ngn ng lp trnh, hoc bn phi trc tip thit k chng. Vic bo tr chng thng rt kh khn, v mt phn cc m chng trnh ln ln vi m html. Khi c li xy ra, vic tm ra li v nh v i cng l mt vn kh khn. V cn nhiu nhng kh khn khc

khc phc cc kh khn trn, ngi ta a ra m hnh 2 hay cn gi l m hnh MVC (Model-ViewControllor). Tc l: Tng ng vi mt trang JSP ngy xa, by gi ngi ta tch n ra lm ba thnh phn: M hnh Khung nhn B iu khin. Cc thnh phn trn lm vic nh sau: M hnh: M hnh l cc lp java c nhim v: Nhn cc yu cu t khung nhn Thi hnh cc yu cu ( tnh ton, kt ni CSDL ) Tr v cc gi tr tnh ton cho View.

Khung nhn: Bao gm cc m tng t nh JSP hin th form nhp liu, cc kt qu tr v t M hnh B iu khin: ng b ho gia Khung nhn v M hnh. Tc l vi mt trang JSP ny th s tng ng vi lp java no x l n v ngc li, kt qu s tr v trang jsp no. Nh vy, chng ta c th tch bit c cc m java ra khi m html. Do vy, n gii quyt c cc kh khn nu ra trong vic lp trnh c in. Ngi thit k giao din v ngi lp trnh java c th mang tnh cht c lp tng i. Vic debug hay bo tr s d dng hn, vic thay i cc theme ca trang web cng d dng hn

II/ Tm hiu ngn ng LinQ


LINQ l mt s sng to mi trong .NET Framework 3.5 (Language Integrate Query) l mt tp hp m rng ngn ng cho php thc hin cc truy vn trong ngn ng C# 2008 v Visual Basic 2008. LINQ cho php select, filter, sort, group v transform data t cc ngun data source khc nhau theo mt cch chung. LINQ to Objects thc hin truy vn cc i tng, LINQ to DataSet thc hin truy vn DataSet, LINQ to SQL thc hin truy vn n c s d liu SQL Server m khng phi vit code, LINQ to XML c d liu t nh dng XML. V d, nu khng dng LINQ cho, cn ly tt c hng ha bt u bng B th ta c code sau:
DanhMucHangHoa dm = new DanhMucHangHoa(); List<HangHoaChiTiet> dshh = dm.LayDanhMucHangHoa(); List<HangHoaChiTiet> dmhh = new List<HangHoaChiTiet>(); foreach (HangHoaChiTiet hh in dshh) { if (hh.HangHoa_TenHang.ToString().StartsWith("B")) { dmhh.Add(hh); } }

Nu s dng LINQ trong m lp trnh :


List<HangHoaChiTiet> dshh = dm.LayDanhMucHangHoa(); IEnumerable<HangHoaChiTiet> dmhh; dmhh = from hh in dshh where hh.HangHoa_TenHang.ToString().StartsWith("B") select hh; GridView1.DataSource = dshh; GridView1.DataBind();

II/ By gi chng ta s bt u xy dng mt v d nh cho m hnh MVC:


y chng ta s xy dng MVC vi ASP.Net v SQL server xy dng c v d chng ta cn cc cng c phn mm h tr sau : + Visual studio 2010 . ( Nu l bn 2008 th cn ci thm b MVC 2) +SQL server 2005 I/ To c s d liu : Chng ta m SQL server ln kt ni vi server Chng ta s to mt c s d liu tn l: NewsDaTa

Database ca chng ta gm 2 bng : Tbl_CatNews: Id(int), NameLoaiTin( nvarchar(200) ) Tbl_News : IdTin(int), NameLoaiTin(int), NameTin(nvarchar(200), ChiTiet( nvarchar(max) ) Ch : Vi ct Id v IdTin chng ta t ng tng chn identity Specification (yes), is identity(yes). II/Chng ta bt u i vo xy dng ng dng : 1. Khi to Project: +M Visual studio ln chon file => new =>project to 1 ng dng mi . +Trong phn Projec types => web . Bn bng Templates => ASP.Net MVC 2 web Application ( xem hnh 1.1)

Hnh 1.1 +Chng trnh s hi ta c to bn test hay khng, nu c bn chn (yes) cn khng th chn (No). y ti chn (No). +Trong ca s Solution Explorer ta thy c 5 th mc : Content : Cha nhng file css ca chng ta , Controllers : Cha nhng file iu khin Models: cha nhng file Models nh x d liu . Scripts: cha nhng file JS h thng Views : Cha nhng file hin th aspx

2 . Kt ni CSDL vi ng dng + Trn thanh menu ca vs2008 chn tools =>conlect to server => nhp Server name ca ti y l ( PC2012032901GDH\SQLEXPRESS ) + Trn Menu Tools => conlect to database => Nhp Servername v chon database cn kt ni . y ti chn database va to trn l NewData + Click phi chut vo folder Models => add=> chn Class +Chn LINQ to SQL classes ( V chng ta truy xut d liu bng LINQ) . t tn cho file Class l DataClasses.dbml . +Trn thanh server explorer ko th cc bng ca database sang trang Dataclasses.dbml

Hnh 2.2 + Gi chng ta to lin kt gia cc bng click phi chut vo trang Dataclasses.dbml => Add=>Association. Trong bng kt ni parentclass l bng cha childclass l bng con y ti chn bng CatNew l cha cn News l con .v kt ni gia Id(CatNew) Vi IDCatNew(New). 3. X l Controller By gi ta x l bng CatNew : Ti folder controller click chut phi chn Add => controller, t tn cho controlle (ch tn ca lp x l phi theo sau bng t Controller ) ti t tn lp x l bng Catnew ca ti l CatnewController.cs ( nu bn mun trnh bin dch t to cc hm Create, Update, Delete th bn chn mc Add action methods bn di ) Gi ta to file CatNewControllers.cs nh sau : CatNewControllers.cs __________________________________________________________________________________________
using using using using using // Ta System; System.Collections.Generic; System.Linq; System.Web; System.Web.Mvc; phi thm dng ny mi c th tham chiu n lp

// CSDL trong folder Models using TinTucs.Models; namespace TinTucs.Controllers { public class CatNewController : Controller { // Kt ni d liu DataTin DataTinDataContext data = new DataTinDataContext(); //--------------------------------------------------------//Hm Index truynd liu sang trang Index.aspx public ActionResult Index() { var All_Loaitin =from tt in data.CatNews select tt; return View(All_Loaitin); } //--------------------------------------------------------------------------// //Hm Details truyn d liu sang trang Details.aspx //Vi tham s c truyn l ID(ca loi tin trong bng CatNew) public ActionResult Details(int id) { var Details_tin=data.CatNews.Where(m=>m.Id==id).First(); return View(Details_tin); } //--------------------------------------------------------------------------// // GET: /CatNew/Create //Hm Create (get )to khung cho ngi s dng nhp liu public ActionResult Create() { return View(); } //---------------------------------------------------------------------------// // POST: /CatNew/Create //Hm Create(Post) x l d liu c chuyn v t trang Create.aspx //v tr v kt qu [HttpPost] public ActionResult Create(FormCollection collection, CatNew ltin) { // To bin CB_Loaitin , CB_GhiChu v gn gi tr ca ngi dng // nhp vo t from trong trang Create.aspx var CB_Loaitin=collection["LoaiTin"]; var CB_Ghichu = collection["GhiChu"]; //Nu CB_Loaitin c gi tr == null ( trng ) if(string.IsNullOrEmpty(CB_Loaitin)) { ViewData["Loi"]=" Loi Tin khng c trng "; } else { ltin.LoaiTin= CB_Loaitin; ltin.GhiChu = CB_Ghichu; data.CatNews.InsertOnSubmit(ltin); //Thc hin to mi data.SubmitChanges();

return RedirectToAction("Index"); } return this.Create(); } //---------------------------------------------------------------------------// // GET: // Hm Edit(get) truyn thng s ca i tng sang trang Edit.aspx // Vi thng s l id. public ActionResult Edit(int id) { var EB_tin = data.CatNews.First(m => m.Id == id); return View(EB_tin); } //---------------------------------------------------------------------------// // POST: /CatNew/Edit/5 // Hm Edit(post) thc hin update d liu // t trang Edit.aspx khi Click Submits [HttpPost] public ActionResult Edit(int id, FormCollection collection) { // To mt bin Ltin gn vi i tng c id=id truyn vo var Ltin = data.CatNews.First(m => m.Id == id); var E_Loaitin = collection["LoaiTin"]; var E_GhiChu = collection["GhiChu"]; //v ta sa i tng ln Id ca bin Ltin = Id chuyn vo . Ltin.Id = id; // Nu ngi dng phn Loi Tin trng bo li if (string.IsNullOrEmpty(E_Loaitin)) { ViewData["Loi"] = "Loi Tin khng c trng "; } // Ngc li gn cc trng ca bin Ltin bng cc gi tr ca //ngi dng nhp vo else { Ltin.LoaiTin = E_Loaitin; Ltin.GhiChu = E_GhiChu; // Thc hin updat UpdateModel(Ltin); data.SubmitChanges(); return RedirectToAction("Index"); } return this.Edit(id); } //------------------------------------------------------------------------// // GET: /CatNew/Delete/5 // Hm Delete ( get ) a d liu ca i tng cn xa // ln trang Delete cho ngi dng xem . Tham s truyn vo l id public ActionResult Delete(int id) { var D_tin = data.CatNews.First(m => m.Id == id); return View(D_tin);

} // POST: /CatNew/Delete/5 // Hm Delete ( post ) thc thi lnh xa i tng khi ngi dng // click xa t trang Delete.aspx . Vi tham s Id [HttpPost] public ActionResult Delete(int id, FormCollection collection) { // To bin D_Tin gn vi di dng c ID bng vi ID tham s var D_tin = data.CatNews.Where(m => m.Id == id).First(); //xa data.CatNews.DeleteOnSubmit(D_tin); data.SubmitChanges(); return RedirectToAction("Index"); } } }

3. X l View +Ta c file CatNewControllers.cs trn th mc Controlesrs th tng ng di th mc View ta cng phi c th mc tn CatNew cha cc trang .aspx cho cc hm thuc file CatNewControllers.cs. + Click phi chut vo View => add => New Folder. +Trong File CatNewControllers.cs trn ta c 5 hm chnh : Index : Hin th danh sch Details : Hin th chi tit Create : To mi Edit : Sa Delete : Xa Tng ng vi 5 hm ny ta s c 5 trang hin th aspx trong folder CatNew: Clickchut phi vo tn hm ( ti chn l hm Index ) => add view

Hnh 2.3 Trong bng add view ta in cc thng s Viewname : tn ca trang aspx ( trng tn vi hm ta ang to view) View data class : lp d liu , controllers x l kt ni vi trang View content : kiu hin th cho trang ( Trang Index ca ti s hin th ra list Loi Tin ln ti chn l List) Select master page : khu vc hin th ( ti chn l trang Master) + Khi visual studio s cho chng ta 1 trang aspx trong th mc View/CatNew chng ta ch cn chnh sa theo ng mnh : Index.aspx __________________________________________________________________________________________
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<TinTucs.Models.CatNew>>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Index </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Index</h2> <table> <tr> <th></th> <th> ID </th>

<th> Loi Tin </th> <th> Ghi ch </th> </tr> <% foreach (var item in Model) { %> <tr> <td> // Th Html.ActionLink dng link n 1 trang khc // ( Tn Link hin th / Th mc ) than s chuyn ra l ( id = id.Item) <%= Html.ActionLink("Sa", "Edit", new { id=item.Id }) %> | <%= Html.ActionLink("Chi Tit", "Details", new { id=item.Id })%> | <%= Html.ActionLink("Xa", "Delete", new { id=item.Id })%> </td> <td> // Hin th ID ca i tng vi th Html.Encode <%= Html.Encode(item.Id) %> </td> <td> <%= Html.Encode(item.LoaiTin) %> </td> <td> <%= Html.Encode(item.GhiChu) %> </td> </tr> <% } %> </table> <p> <%= Html.ActionLink("Thm Mi", "Create") %> </p> </asp:Content>

Tng t chng ta c cc trang aspx cho cc hm khc Details.aspx


<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TinTucs.Models.CatNew>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Details </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Details</h2> <h3>Are you sure you want to delete this?</h3> <fieldset> <legend>Fields</legend>

<div class="display-label">Id</div> <div class="display-field"><%= Html.Encode(Model.Id) %></div> <div class="display-label">LoaiTin</div> <div class="display-field"><%= Html.Encode(Model.LoaiTin) %></div> <div class="display-label">GhiChu</div> <div class="display-field"><%= Html.Encode(Model.GhiChu) %></div> </fieldset> <% using (Html.BeginForm()) { %> <p> <%= Html.ActionLink("Sa", "Edit", new { id = Model.Id })%> <%= Html.ActionLink("Back to List", "Index") %> </p> <% } %> </asp:Content>

Create.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TinTucs.Models.CatNew>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Create </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Create</h2> <% using (Html.BeginForm()) {%> <%= Html.ValidationSummary(true) %> <fieldset> <legend>Fields</legend> <p> Loi Tin : <%=Html.TextBox("LoaiTin") %> <%=ViewData["Loi"] %> </p> <p> Ghi Ch : <%=Html.TextBox("GhiChu") %> <%=ViewData["Loi2"] %> </p> <p> <input type="submit" value="Create" /> </p> </fieldset> <% } %>

<div> <%= Html.ActionLink("Back to List", "Index") %> </div> </asp:Content>

Edit.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TinTucs.Models.CatNew>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Edit </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Edit</h2> <% using (Html.BeginForm()) {%> <%= Html.ValidationSummary(true) %> <fieldset> <legend>Fields</legend> <div class="editor-label"> <%= Html.HiddenFor(model => model.Id) %> </div> <div class="editor-field"> <%= Html.HiddenFor(model => model.Id) %> <%= Html.ValidationMessageFor(model => model.Id) %> </div> <div class="editor-field"> Loi Tin : <%= Html.TextBox("LoaiTin") %> <%= Html.ValidationMessageFor(model => model.LoaiTin) %> <%=ViewData["Loi"] %> </div> <div class="editor-field"> Ghi Ch: <%= Html.TextBox("GhiChu") %> <%= Html.ValidationMessageFor(model => model.GhiChu) %> </div> <p> <input type="submit" value="Save" /> </p> </fieldset> <% } %> <div> <%= Html.ActionLink("Back to List", "Index") %> </div>

</asp:Content>

Delete.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TinTucs.Models.CatNew>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Delete </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Delete</h2> <h3>Bn thc s mun xa th loi tin ny :</h3> <fieldset> <legend>Fields</legend> <div class="display-label">Id</div> <div class="display-field"><%= Html.Encode(Model.Id) %></div> <div class="display-label">LoaiTin</div> <div class="display-field"><%= Html.Encode(Model.LoaiTin) %></div> <div class="display-label">GhiChu</div> <div class="display-field"><%= Html.Encode(Model.GhiChu) %></div> </fieldset> <% using (Html.BeginForm()) { %> <p> <input type="submit" value="Delete" /> | <%= Html.ActionLink("Back to List", "Index") %> </p> <% } %> </asp:Content>

Vy l chng ta x l xong phn CatNew gi chng ta chuyn qua phn new:

Gi chng ta chuyn sang x l phn News To mt trang NewsControllers.cs NewsControllers.cs

using System;

using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; // Thm using TinTucs.Models; kt ni vi classdata ti folder Models using TinTucs.Models; namespace TinTucs.Controllers { public class NewsController : Controller { //Kt ni CSDL DataTinDataContext data = new DataTinDataContext(); //Hm Index truyn d liu sang trang Index.aspx // hin th danh sch tin tc public ActionResult Index() { // Khi to bin All_tin v gn vi i tng trong bng New var All_tin = from tt in data.News select tt; //Truyn d li sang trang Index.aspx return View(All_tin); } //-----------------------------------------------------------------------------------// //Hm Details truyn d liu ca mt i tng sang trang Details.aspx // vi tham s truyn vo l Id ca i tng public ActionResult Details(int id) { var D_tin = data.News.First(m => m.IdNew == id); return View(D_tin); } //---------------------------------------------------------------------------------// // GET: /News/Create //Hm Create(get) To mt khung cho ngi dng nhp liu public ActionResult Create() { var L_tin = from lt in data.CatNews select lt; ViewData["ListLoaiTinID"]= new SelectList(data.CatNews,"Id","LoaiTin"); return View(); } //--------------------------------------------------------------------------------// // POST: /News/Create //Hm Create(post) Nhn d liu t trang Create v thc hin vic //to mi d liu [HttpPost] public ActionResult Create(FormCollection collection, New TTin) { // Gn cc gi t ngi dng nhp liucho cc bin var N_tin = collection["NameTin"]; var L_tin = Int32.Parse(collection["ListLoaiTinID"]); var C_tin = collection["ChiTiet"]; if (String.IsNullOrEmpty(N_tin))

{ ViewData["Loi"] = "Tn tin khng c trng"; } else if (String.IsNullOrEmpty(C_tin)) { ViewData["Loi1"]="Bn hy nhp ni dung cho tin"; } else { //Gn gi tr cho i tng c to mi (TTin) TTin.IdCatNew = L_tin; TTin.NameTin = N_tin; TTin.ChiTiet = C_tin; //Thc hin to mi data.News.InsertOnSubmit(TTin); data.SubmitChanges(); return RedirectToAction("Index"); } return this.Create(); } //--------------------------------------------------------------------------------// // GET: /News/Edit/5 //Hm Edit (get) truyn d liu ti file Edit.aspx // Vi tham s c truyn vo l IDNew public ActionResult Edit(int id) { var E_tin = data.News.First(m => m.IdNew == id); var L_tin = from lt in data.CatNews select lt; ViewData["ListLoaiTinID"] = new SelectList(data.CatNews, "Id", "LoaiTin"); return View(E_tin); } // POST: /News/Edit/5 //Hm Edit(post) nhn d li m ngi dng nhp t trang Edit.aspx // v thc hin sa i [HttpPost] public ActionResult Edit(int id, FormCollection collection, New Etin) { var N_tin = collection["NameTin"]; var L_tin = Int32.Parse(collection["ListLoaiTinID"]); var C_tin = collection["ChiTiet"]; if (String.IsNullOrEmpty(N_tin)) { ViewData["Loi"] = "Tn tin khng c trng"; } else if (String.IsNullOrEmpty(C_tin)) { ViewData["Loi1"] = "Bn hy nhp ni dung cho tin"; } else { Etin.IdCatNew = L_tin; Etin.NameTin = N_tin; Etin.ChiTiet = C_tin;

UpdateModel(Etin); data.SubmitChanges(); return RedirectToAction("Index"); } return this.Edit(id); } //------------------------------------------------------------------------------// // GET: /News/Delete/5 //Hm Delete(get) truyn d liu ti trang Delete.aspx // vi tham s ID ca mc cn xa public ActionResult Delete(int id) { var D_Tin = data.News.First(m => m.IdNew == id); return View(D_Tin); } // // POST: /News/Delete/5 //Hm Delete(get) thc hin lnh xa vi tham s truyn vo l id mc cn xa [HttpPost] public ActionResult Delete(int id, FormCollection collection) { var D_tin = data.News.Where(m => m.IdNew == id).First(); data.News.DeleteOnSubmit(D_tin); data.SubmitChanges(); return RedirectToAction("Index"); } } }

Tng t nh phn CatNew chng ta s c cc trang aspx nh sau trong th mc News : Index.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<TinTucs.Models.New>>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Index </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Danh sch tin tc</h2> <table> <tr> <th> ID </th> <th>

Loi Tin </th> <th> Tn Tin </th> <th> Chi Tit </th> <th> La Chn </th> </tr> <% foreach (var item in Model) { %> <tr> <td> <%= </td> <td> <%= </td> <td> <%= </td> <td> <%= </td> <td> <%= <%= | <%= Html.ActionLink("Xa", "Delete", new { id=item.IdNew })%> </td> </tr> <% } %> </table> <p> <%= Html.ActionLink("To Mi", "Create") %> </p> </asp:Content> Html.Encode(item.IdNew) %> Html.Encode(item.CatNew.LoaiTin) %> Html.Encode(item.NameTin) %> Html.Encode(item.ChiTiet) %> Html.ActionLink("Sa", "Edit", new { id=item.IdNew }) %> | Html.ActionLink("Chi Tit", "Details", new { id=item.IdNew })%>

Details.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TinTucs.Models.New>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

Details </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <fieldset> <legend>Thng Tin Chi Tit</legend> <div class="display-label">Id</div> <div class="display-field"><%= Html.Encode(Model.IdNew) %></div> <div class="display-label">Loi Tin</div> <div class="display-field"><%= Html.Encode(Model.CatNew.LoaiTin) %></div> <div class="display-label">Tn Tin</div> <div class="display-field"><%= Html.Encode(Model.NameTin) %></div> <div class="display-label">Chi Tit</div> <div class="display-field"><%= Html.Encode(Model.ChiTiet) %></div> </fieldset> <p> <%= Html.ActionLink("Sa", "Edit", new { id=Model.IdNew }) %> | <%= Html.ActionLink("Quay li danh sch", "Index") %> </p> </asp:Content>

Edit.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TinTucs.Models.New>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Edit </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <% using (Html.BeginForm()) {%> <%= Html.ValidationSummary(true) %> <fieldset> <legend>Sa</legend> <p> Tn Tin: <%=Html.TextBox("NameTin")%> <%=ViewData["Loi"] %> </p> <p> Loi Tin : <%=Html.DropDownList("ListLoaiTinID") %> <%=ViewData["Loi1"]%> </p>

<p> Chi Tit: <%=Html.TextBox("ChiTiet") %> </p> <p> <input type="submit" value=Save" /> </p> </fieldset> <% } %> <div> <%= Html.ActionLink("Quay li danh sch", "Index") %> </div> </asp:Content>

Create.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TinTucs.Models.New>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Create </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Create</h2> <% using (Html.BeginForm()) {%> <%= Html.ValidationSummary(true) %> <fieldset> <legend>To Mi</legend> <p> Tn Tin: <%=Html.TextBox("NameTin")%> <%=ViewData["Loi"]%> </p> <p> Loi Tin : <%=Html.DropDownList("ListLoaiTinID") %> </p> <p> Chi Tit: <%=Html.TextBox("ChiTiet") %> </p> <p> <input type="submit" value="Create" /> </p> </fieldset> <% } %>

<div> <%= Html.ActionLink("Quay li danh sch", "Index") %> </div> </asp:Content>

Delete.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TinTucs.Models.New>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Delete </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <fieldset> <legend>Bn c tht s mun xa !!</legend> <div class="display-label">ID</div> <div class="display-field"><%= Html.Encode(Model.IdNew)%></div> <div class="display-label">Loi Tin</div> <div class="display-field"><%= Html.Encode(Model.CatNew.LoaiTin) %></div> <div class="display-label">Tn Tin :</div> <div class="display-field"><%= Html.Encode(Model.NameTin) %></div> <div class="display-label">Chi Tit:</div> <div class="display-field"><%= Html.Encode(Model.ChiTiet) %></div> </fieldset> <% using (Html.BeginForm()) { %> <p> <input type="submit" value="Xa" /> | <%= Html.ActionLink("Quay li danh sch", "Index") %> </p> <% } %> </asp:Content>

Sau khi hon thnh chng ta s phi sa i li file Site.Master c ng dn ti cc trang m chng ta va to . Site.Master
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>

<link href="../../Content/Site.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="page"> <div id="header"> <div id="title"> <h1>My MVC Application</h1> </div> <div id="logindisplay"> <% Html.RenderPartial("LogOnUserControl"); %> </div> <div id="menucontainer"> <ul id="menu"> // Chng ta s sa li phn menu ca trang Master y : // th Html.ActionLink cho php chng ta kt ni vi mt trang kh // (Tn hin th ca lin kt , Tn trang aspx hin th , Tn Folder cha) <li><%= Html.ActionLink("Home", "Index", "Home")%></li> <li><%= Html.ActionLink("Thm Loi Tin", "Create", "CatNew")%></li> <li><%= Html.ActionLink("Thm Tin Tc", "Create", "News")%></li> <li><%= Html.ActionLink("Loi Tin Tc", "Index", "CatNew")%></li> <li><%= Html.ActionLink("Tin Tc", "Index", "News")%></li> </ul> </div> </div> <div id="main"> <asp:ContentPlaceHolder ID="MainContent" runat="server" /> <div id="footer"> </div> </div> </div> </body> </html>

Kt qu t c : Trang

Chc bn thnh cng !

You might also like