ModelState中总是有效 [英] ModelState always valid

查看:150
本文介绍了ModelState中总是有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到的东西看似很简单,没​​有工作。

I've got something seemingly very simple not working.

我有一个模型

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

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

视图模型

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

视图

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

和控制器

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

不管是什么我作为标题(null,或者一个字符串> 10个字符)输入,ModelState.IsValid始终是真实的。在客户对象的标题字段的值,所以该数据被传递左右,但没有被验证?

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?

任何线索?

推荐答案

想通了,有人监守我引用System.ComponentModel.DataAnnotations 3.6,而不是3.5。从我收集,3.6仅供WCF RIA服务。

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.

这篇关于ModelState中总是有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆