现在的位置: 首页 > 综合 > 正文

ASP.NET MVC V2 Preview 1 新特性初探

2013年06月27日 ⁄ 综合 ⁄ 共 5334字 ⁄ 字号 评论关闭

 

Asp.net MVC1.0正式版刚刚出来才几个月,ASP.NET MVC ASP.NET MVC2 Preview 1 Released 就跟我们见面了 您可以点击这儿下载。该版本可以和以前的Asp.net MVC1.0 兼容,安装了ASP.NET MVC2后,打开vs2008 ,新建项目,可以看到同时存在1.02的模板,如下图:  


      在ASP.NET MVC2版中,包含了区域(Area)、数据标记验证(Data Annotation Validation)、强类型UI辅助方法和模板化辅助方法(TemplatedHelper)四个特性,下面就分别来看下这个四个特性。

      区域(Areas

      区域(Areas)可以将Asp.net MVC应用的功能进行分割和组合,它可以将ControllerView进行分组管理。如下图中就是将博客(blogs)和相册(Albums)的部分通过Areas提出来单独管理。遗憾的是在预览版1中还没有任何工具的支持,所以Areas项目必须通过手动添加和配置。

    
      数据标记验证(Data Annotation Validation
      DataAnnotation提供了一个简单的方式,在应用中的Model(模型)和ViewModel(视图模型)类中添加验证规则,在ASP.NET MVC中有自动的绑定和UI辅助方法验证支持。首先创建一个实体类Users,代码如下:

 1 namespace Mvc2Demo.Models
 2 {
 3     public class Users
 4     {
 5         [Required(ErrorMessage="用户名不能为空!")]
 6         public String    UserName   { getset; }
 7 
 8         [Range(0,150,ErrorMessage="年龄必须在0-150之间!")]
 9 [Required(ErrorMessage="年龄不能为空!")]
10         public Int32     Age        { getset; }
11 
12         [Required(ErrorMessage="邮箱地址不能为空!")]
13         [RegularExpression("//w+([-+.']//w+)*@//w+([-.]//w+)*//.//w+([-.]//w+)*",ErrorMessage="邮箱格式不正确,请重新填写!")]   
14         public String    Email      { getset; }
15     }
16 }
17 

      

      使用Required RegularExpression 等属性需要引用命名空间   using System.ComponentModel.DataAnnotations;

      在Controllers目录下创建UserController.cs,代码如下:

 

 1 namespace Mvc2Demo.Controllers
 2 {
 3     public class UserController : Controller
 4     {
 5         public ActionResult Index()
 6         {
 7             return View();
 8         }
 9         public ActionResult Create()
10         {
11             Users user = new Users();
12             return View(user);
13         }
14        //在ASP.NET MVC2预览一版中使用[HttpPost] 1.0版中使用[AcceptVerbs(HttpVerbs.Post)]
15         [HttpPost]
16         public ActionResult Create(Users user)
17         {
18             if (!ModelState.IsValid)
19             {
20                 return View(user);
21             }
22             return View("Success");
23         }
24     }
25 }

 

      在上面的任意一个Create方法上右击,选择Add View … ,在弹出的对话框中做相应的设置,然后点击Add,就会在View下添加相应的目录和页面文件,如下图:

    
      添加完后,系统会自动为Create视图生成如下代码:

 

 1 <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Mvc2Demo.Models.Users>" %>
 2 
 3 <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
 4     Create
 5 </asp:Content>
 6 
 7 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 8     <h2>Create </h2>
 9     <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again."%>
10     <% using (Html.BeginForm()) {%>
11         <fieldset>
12             <legend>Fields</legend>
13             <p>
14                 <label for="UserName">UserName:</label>
15                 <%= Html.TextBox("UserName"%>
16                 <%= Html.ValidationMessage("UserName""*"%>
17             </p>
18             <p>
19                 <label for="Age">Age:</label>
20                 <%= Html.TextBox("Age"%>
21                 <%= Html.ValidationMessage("Age""*"%>
22             </p>
23             <p>
24                 <label for="Email">Email:</label>
25                 <%= Html.TextBox("Email"%>
26                 <%= Html.ValidationMessage("Email""*"%>
27             </p>
28             <p>
29                 <input type="submit" value="Create" />
30             </p>
31         </fieldset>
32 
33     <% } %>
34     <div>
35         <%=Html.ActionLink("Back to List""Index"%>
36     </div>
37 </asp:Content>

      运行后的界面如下图:
   

      在文本框中填入一些非法数据后,点击“创建用户”后 非法数据不能通过验证,将在页面中显示错误提示信息,如下图:
   

      强类型UI辅助方法

      在ASP.NET MVC2预览1版中提供了新的Html辅助方法,这些辅助方法允许你在引用视图模板的模型对象时使用强类型的lambda表达式,如<%= Html.EditorFor(u=>u.UserName) %> ,而且lambda表达式也提供了良好的智能提示功能,如下图:

 

   

      在ASP.NET MVC2的预览版1中提供了Html.EditorForHtml.LabelFor(), Html.DisplayFor() 辅助方法的内置支持。在以后的版本中将会提供更多这种辅助方法的支持。下面的代码是使用了辅助方法的“Create”视图模版。

 

 1 <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Mvc2Demo.Models.Users>" %>
 2 <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
 3     Create
 4 </asp:Content>
 5 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 6     <h2>Create </h2>
 7     <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again."%>
 8     <% using (Html.BeginForm()) {%>
 9         <fieldset>
10             <legend>Fields</legend>
11             <p>
12                 <%= Html.LabelFor(u=>u.UserName) %>
13                 <%= Html.EditorFor(u=>u.UserName) %>
14                 <%= Html.ValidationMessage("UserName""*"%>
15             </p>
16             <p>
17                 <%= Html.LabelFor(u=>u.Age) %>
18                 <%= Html.EditorFor(u=>u.Age) %>
19                 <%= Html.ValidationMessage("Age""*"%>
20             </p>
21             <p>
22                 <%= Html.LabelFor(u=>u.Email) %>
23                 <%= Html.EditorFor(u=>u.Email) %>
24                 <%= Html.ValidationMessage("Email""*"%>
25             </p>
26             <p>
27                 <input type="submit" value="Create" />
28             </p>
29         </fieldset>
30     <% } %>
31     <div>
32         <%=Html.ActionLink("Back to List""Index"%>
33     </div>
34 </asp:Content>

           Html.EditorFor()支持任何的数据类型,而且会根据数据类型做出判断来生成不同的html代码,现在在实体类Users中添加一个bool类型的属性IsOnLine,如下图:
   

抱歉!评论已关闭.