2016-08-26 45 views
0

出于某种原因,它在表格标题的索引视图中工作,但不适用于创建视图。显示注释在创建视图中不起作用

这里是我的控制器模型的相关部分:

[Required(ErrorMessage = "Status is required.")] 
    [Display(Name = "Status")] 
    public int StatusId { get; set; } 

我也有懒加载:

public virtual Status status { get; set; } 

我甚至把它添加到状态模式:

[Required(ErrorMessage = "Status is required.")] 
    [MaxLength(50)] 
    [Display(Name = "Status")] 
    public string StatusName { get; set; } 

这是它的意见:

 <div class="form-group"> 
      @Html.LabelFor(model => model.StatusId, "StatusId", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("StatusId", null, htmlAttributes: new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.StatusId, "", new { @class = "text-danger" }) 
      </div> 

然而,它并不作为“状态”的实际创建页面显示:

enter image description here

同样的事情发生与另一个在同一个模型。这是怎么回事?

回答

2

因为您正在使用下面的超载,您明确提供要显示的文本。

public static MvcHtmlString LabelFor<TModel, TValue>(
    this HtmlHelper<TModel> html, 
    Expression<Func<TModel, TValue>> expression, 
    string labelText, 
    IDictionary<string, object> htmlAttributes 
) 

在正在使用的过载的第二个参数是将被显示在标签中labelText

使用此重载

public static MvcHtmlString LabelFor<TModel, TValue>(
    this HtmlHelper<TModel> html, 
    Expression<Func<TModel, TValue>> expression, 
    object htmlAttributes 
) 

所以,你的代码将

@Html.LabelFor(model => model.StatusId, new { @class = "control-label col-md-2" }) 
+0

哇哦我没有看到过载,我不知道为什么它产生的。 –