Asp.net MVC學習日記十四(頁面提交驗證)

來源:互聯網
上載者:User

首先看看mvc自己是怎麼做的

1、建立Person類

 public class Person
    {
        [DisplayName("First Name"), StringLength(10)]
        public string FirstName { get; set; }
        [DisplayName("Middle Name"), StringLength(50)]
        public string MiddleName { get; set; }
        [DisplayName("Last Name"), StringLength(50), Required]
        public string LastName { get; set; }
        [DisplayName("Birth Date"), DataType(DataType.Date)]
        public DateTime BirthDate { get; set; }
        [DataType(DataType.EmailAddress), Required]
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Postcode { get; set; }
        [DataType(DataType.MultilineText)]
        public string Notes { get; set; }
    }

2、HomeController中

  public ActionResult Index()
        {
            ViewData["Message"] = "歡迎使用 ASP.NET MVC!";

            return View(new Person());
        }

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            var person = new Person();
            TryUpdateModel(person);
            return View(person);
        }

3、Index.aspx中

<% using (Html.BeginForm()) {%>
<%: Html.EditorForModel() %>
<input type="submit" name="submit" value="Submit" />
<% } %>

再看看結合JQuery是怎麼乾的

1、去http://code.google.com/p/arscloud/source/browse/trunk/MvcFutures/MvcFutures/?r=7 下載一個MicrosoftMvcJQueryValidation.js

2、添加   

     <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>

     <script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
     <script src="../../Scripts/MicrosoftMvcJQueryValidation.js" type="text/javascript"></script>

3、在Index.aspx

<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) {%>
<%: Html.EditorForModel() %>
<input type="submit" name="submit" value="Submit" />
<% } %>

 

 

自訂驗證規則類

1、建立類EmailAttribute

public class EmailAttribute : RegularExpressionAttribute {
public EmailAttribute() : base(
@"^[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,6}$") {
ErrorMessage = "Email is invalid";
}
}

2、Models/Person.cs:
public DateTime BirthDate { get; set; }
[DataType(DataType.EmailAddress), Required, Email]
public string Email { get; set; }

3、Global.asax.cs:
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(EmailAttribute),
typeof(RegularExpressionAttributeAdapter));
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.