View物件以IView與IViewDataContainer等介面為主,並且以ASP.NET的各式前端介面為主要輸出工具,基於MVC的View彈性化設計考量,以往在ASP.NET Web Form的程式碼與HTML分離模式將不再存在,而是將程式碼與HTML混合的方式設計,讓開發人員可以更精確的對View進行控制,而目前 ASP.NET MVC 支援的 View 有下列幾種[5]:
<%@MasterLanguage="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><headrunat="server"><title><asp:ContentPlaceHolderID="TitleContent"runat="server"/></title><linkhref="../../Content/Site.css"rel="stylesheet"type="text/css"/></head><body><divclass="page"><divid="header"><divid="title"><h1>MyMVCApplication</h1></div><divid="logindisplay"><%Html.RenderPartial("LogOnUserControl");%></div><divid="menucontainer"><ulid="menu"><li><%=Html.ActionLink("Home","Index","Home")%></li><li><%=Html.ActionLink("About","About","Home")%></li></ul></div></div><divid="main"><asp:ContentPlaceHolderID="MainContent"runat="server"/><divid="footer"></div></div></div></body></html>
在 ASP.NET MVC 中,Model 相對不設限,可以使用內建的資料結構以及自訂的資料類別,也可以是一個商業物件,因此 Model 的彈性相當大,除了前述的資料結構外,微軟新發展的一些資料存取方式也可以應用在 Model 中,像是ADO.NET Entity Framework與LINQ to SQL等技術。
[AcceptVerbs(HttpVerbs.Post)]publicActionResultCreate(Personperson){if(person.Name.Trim().Length==0){ModelState.AddModelError("Name","Name is required.");}if(person.Age<1||person.Age>200){ModelState.AddModelError("Age","Age must be within range 1 to 200.");}if((person.Zipcode.Trim().Length>0)&&(!Regex.IsMatch(person.Zipcode,@"^\d{5}$|^\d{5}-\d{4}$"))){ModelState.AddModelError("Zipcode","Zipcode is invalid.");}if(!Regex.IsMatch(person.Phone,@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}")){ModelState.AddModelError("Phone","Phone number is invalid.");}if(!Regex.IsMatch(person.Email,@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$")){ModelState.AddModelError("Email","Email format is invalid.");}if(!ModelState.IsValid){returnView("Create",person);}people.Add(person);returnRedirectToAction("Index");}
在 ASP.NET MVC 2.0 中,新增了一個可以直接讓 MVC Framework 針對資料欄位進行驗證控制的模型,稱為 Model Validation,它融合了在 .NET Framework 3.5 SP1 發表的 ASP.NET Dynamic Data Framework 中 Data Annotations (資料記號) 的特性,讓開發人員可以只利用標記的方式來執行驗證,或是利用自訂的程式碼來擴充資料記號的驗證行為。
usingSystem.ComponentModel.DataAnnotations;namespaceMvcDA{[MetadataType(typeof(ProductMD))]publicpartialclassProduct{publicclassProductMD{[StringLength(50),Required]publicobjectName{get;set;}[StringLength(15)]publicobjectColor{get;set;}[Range(0, 9999)]publicobjectWeight{get;set;}// public object NoSuchProperty { get; set; }}}}
<h2>Create</h2><%=Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.")%><%using(Html.BeginForm()){%><fieldset><legend>Fields</legend><p><labelfor="Name">Name:</label><%=Html.TextBox("Name")%>Required
<%=Html.ValidationMessage("Name","*")%></p><p><labelfor="Age">Age:</label><%=Html.TextBox("Age")%>Required
<%=Html.ValidationMessage("Age","*")%></p><p><labelfor="Street">Street:</label><%=Html.TextBox("Street")%><%=Html.ValidationMessage("Street","*")%></p><p><labelfor="City">City:</label><%=Html.TextBox("City")%><%=Html.ValidationMessage("City","*")%></p><p><labelfor="State">State:</label><%=Html.TextBox("State")%><%=Html.ValidationMessage("State","*")%></p><p><labelfor="Zipcode">Zipcode:</label><%=Html.TextBox("Zipcode")%><%=Html.ValidationMessage("Zipcode","*")%></p><p><labelfor="Phone">Phone:</label><%=Html.TextBox("Phone")%>Required
<%=Html.ValidationMessage("Phone","*")%></p><p><labelfor="Email">Email:</label><%=Html.TextBox("Email")%>Required
<%=Html.ValidationMessage("Email","*")%></p><p><inputtype="submit"value="Create"/></p></fieldset><%}%><div><%=Html.ActionLink("Back to List","Index")%></div>