2013-04-29 21 views
6

Xác nhận trường bắt buộc không kích hoạt.MVC Xác nhận trường bắt buộc không hoạt động

mẫu:

public class Client 
    { 
     [Required(ErrorMessage="Name Required")] 
     [DisplayFormat(ConvertEmptyStringToNull = false)] 
     public string Name { get; set; } 
     public string Address { get; set; } 
     public string Mobile { get; set; } 
     public string Telephone { get; set; } 
     public string Fax { get; set; } 
     public string Company { get; set; } 
    } 

Bộ điều khiển:

[HttpPost] 
     public ActionResult Create(Client client) 
     { 
      try 
      { 
       // TODO: Add insert logic here 
       ClientRepository rep = new ClientRepository(); 
       rep.AddClient(client); 
       rep.Save(); 
       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 

Xem:

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Create 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <h2>Add New Client</h2> 

    <% using (Html.BeginForm()) {%> 
     <%: Html.ValidationSummary(true) %> 

     <fieldset> 
      <legend>Fields</legend> 

      <div class="editor-label"> 
       <%: Html.LabelFor(model => model.Name) %> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBoxFor(model => model.Name) %> 
       <%: Html.ValidationMessageFor(model => model.Name) %> 
      </div> 

      <div class="editor-label"> 
       <%: Html.LabelFor(model => model.Address) %> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBoxFor(model => model.Address) %> 
       <%: Html.ValidationMessageFor(model => model.Address) %> 
      </div> 

      <div class="editor-label"> 
       <%: Html.LabelFor(model => model.Mobile) %> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBoxFor(model => model.Mobile) %> 
       <%: Html.ValidationMessageFor(model => model.Mobile) %> 
      </div> 

      <div class="editor-label"> 
       <%: Html.LabelFor(model => model.Telephone) %> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBoxFor(model => model.Telephone) %> 
       <%: Html.ValidationMessageFor(model => model.Telephone) %> 
      </div> 

      <div class="editor-label"> 
       <%: Html.LabelFor(model => model.Fax) %> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBoxFor(model => model.Fax) %> 
       <%: Html.ValidationMessageFor(model => model.Fax) %> 
      </div> 

      <div class="editor-label"> 
       <%: Html.LabelFor(model => model.Company) %> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBoxFor(model => model.Company) %> 
       <%: Html.ValidationMessageFor(model => model.Company) %> 
      </div> 

      <p> 
       <input type="submit" value="Create" /> 
      </p> 
     </fieldset> 

    <% } %> 

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

</asp:Content> 
+0

** [Tại sao xác nhận phía máy chủ và không khách hàng bên] (http://stackoverflow.com/a/16245682/2007801) ** –

+1

@ user2007801 Chỉ cần cho các hồ sơ, bạn phải không bao giờ tin tưởng vào người sử dụng đầu vào. Xác thực phía máy khách có thể bị vô hiệu hóa, thao tác giả, vv, vì vậy việc xác thực phía máy chủ luôn cần thiết. – Oscar

Trả lời

14

Sử dụng ModelState.IsValid p roperty

public ActionResult Create(Client client) 
{ 

    if(ModelState.IsValid) 
    { 
    // TODO: Add code here 
     ClientRepository rep = new ClientRepository(); 
     rep.AddClient(client); 
     rep.Save(); 
     return RedirectToAction("Index"); 
    } 
    return View(client); 
} 
+0

Tôi nghĩ đây không phải là câu trả lời. Đây là một cách khác để xác thực các trường. – hasanaydogar

+1

Tôi không nghĩ vậy. – adatapost