asp.net-mvc - MVC 必需的字段验证不起作用

标签 asp.net-mvc asp.net-mvc-2 validation

未触发必填字段验证。

型号:

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; }
    }

Controller :

 [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();
            }
        }

查看:

<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>

最佳答案

使用ModelState.IsValid属性(property)

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);
} 

关于asp.net-mvc - MVC 必需的字段验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16270377/

相关文章:

.net - 将 jEditable 与 ASP.NET MVC 结合使用(POST)

wcf - MVC2 路由与 WCF ServiceRoute : Html. ActionLink 呈现不正确的链接!

c# - ASP.Net MVC 2 : Using a viewmodel destroys my model binding

xml - 通过 JAXB 验证 XML

validation - 使用 JSF 消息包覆盖 Omnifaces 验证消息?

c# - Azure Web 应用程序。免费比基本版和标准版更快?

ios - 将 JSON 从 iOS 发布到 ASP.NET

c# - 如何将 span 标签放在 Html.ActionLink 中的 anchor 标签内?

jQuery $.ajax 将 null 值传递给 MVC Controller

validation - 规范化/验证数据库中的国际数据集?