ModelState always valid

▼魔方 西西 提交于 2020-01-04 05:31:31

问题


I've got something seemingly very simple not working.

I have got a model

public class Name: Entity
{
    [StringLength(10), Required]
    public virtual string Title { get; set; }
}

public class Customer: Entity
{
    public virtual Name Name { get; set; }
}

a view model

public class CustomerViweModel
{
    public Customer Customer { get; set; }
}

a view

       <% using(Html.BeginForm()) { %>
                    <%= Html.LabelFor(m => m.Customer.Name.Title)%>
                    <%= Html.TextBoxFor(m => m.Customer.Name.Title)%> 
                    <button type="submit">Submit</button>
        <% } %>

and a controller

[HttpPost]
public ActionResult Index([Bind(Prefix = "Customer")] Customer customer)
{
      if(ModelState.IsValid)
           Save
       else
           return View();
 }

No matter what I enter as the title (null, or a string > 10 chars), ModelState.IsValid is always true. The Title field in the Customer object has a value, so the data is being passed around, but not being validated?

Any clues?


回答1:


In your View I don't see any text box or a field allowing to send data to the controller, only a label. Properties will not be validated if they are not posted. Add a textbox, leave it blank and your model won't be valid any more:

<%= Html.TextBoxFor(m => m.Customer.Name.Title)%>

UPDATE:

Here's the code I've used:

Model:

public class Name
{
    [StringLength(10), Required]
    public virtual string Title { get; set; }
}

public class Customer
{
    public virtual Name Name { get; set; }
}

public class CustomerViewModel
{
    public Customer Customer { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index([Bind(Prefix = "Customer")]Customer cs)
    {
        return View(new CustomerViewModel
        {
            Customer = cs
        });
    }
}

View:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyApp.Models.CustomerViewModel>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using(Html.BeginForm()) { %>
        <%= Html.LabelFor(m => m.Customer.Name.Title)%>
        <%= Html.TextBoxFor(m => m.Customer.Name.Title)%> 
        <button type="submit">Submit</button>
    <% } %>
</asp:Content>

When you submit this form a validation error is shown.

Remark1: I've omitted the Entity base class in the models as I don't how does it look.

Remark2: I've renamed the variable in the Index action to cs. I remember that there was some problems with this in ASP.NET MVC 1.0 when you had the prefix and the variable named the same but I am not sure whether this applies here and I think it was fixed.




回答2:


Figured it out, it was becuase I'm referencing System.ComponentModel.DataAnnotations 3.6 instead of 3.5. From what I gather, 3.6 is for WCF RIA services only.



来源:https://stackoverflow.com/questions/2659127/modelstate-always-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!