2011-04-25 131 views
0

所以当我有一个DisplayAttribute装饰在我的模型的一个属性...DisplayAttribute关闭验证消息

[Required, Display(Name = "Some Name")] 
public string SomeProperty { get; set; } 

我使用ValidationMessageFor帮手

时不再收到该领域的验证消息
@Html.ValidationMessageFor(model => model.SomeProperty) 

有什么奇怪的是,如果我使用指定消息的重载,我仍然没有得到消息。任何人都知道这里发生了什么?

回答

0

无法重现。

型号:

public class MyViewModel 
{ 
    [Required, Display(Name = "Some Name")] 
    public string SomeProperty { get; set; } 
} 

控制器:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     var model = new MyViewModel(); 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(MyViewModel model) 
    { 
     return View(model); 
    } 
} 

检视:

@model MyViewModel 
@using (Html.BeginForm()) 
{ 
    @Html.LabelFor(x => x.SomeProperty) 
    @Html.EditorFor(x => x.SomeProperty) 
    @Html.ValidationMessageFor(x => x.SomeProperty) 
    <input type="submit" value="OK" /> 
} 

当表单提交验证错误消息,如果字段留空正确显示。

+0

我唯一能想到的另一件事是我有一个类型编辑器模板(HttpPostedFileBase),输入是type = file。我无法让它工作。该字段变为红色(默认的CSS样式),但没有消息。 – JasonCoder 2011-04-26 16:24:15