0

我有一个供应商实体,其中包含

ID - int
Status - string
Name - string
CreateDate- datetime

我正在使用部分类方法为上述实体创建数据注释。如此处所述

namespace TemplateEx.Models
{
    [MetadataType(typeof(SupplierMetadata))]
    public partial class Supplier
    {
        // Note this class has nothing in it.  It's just here to add the class-level attribute.
    }

    public class SupplierMetadata
    {
        // Name the field the same as EF named the property - "FirstName" for example.
        // Also, the type needs to match.  Basically just redeclare it.
        // Note that this is a field.  I think it can be a property too, but fields definitely should work.

        [Required]
        [Display(Name = "Supplier Name")]
        public string Name;
    }
}

我定义了一个控制器动作如下

 public ViewResult Details(int id)
    {
        Supplier supplier = db.Suppliers1.Single(s => s.ID == id);
        return View(supplier);
    }

当我为此操作创建视图并为供应商实体选择详细信息脚手架时,以下是我得到的视图

@model TemplateEx.Models.Supplier

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
    <legend>Supplier</legend>

    <div class="display-label">CreateDate</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CreateDate)
    </div>

    <div class="display-label">Status</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Status)
    </div>

    <div class="display-label">Name</div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Name)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

请注意 model.Name 有一个“名称”标签而不是“供应商名称”。我做错了什么?

4

1 回答 1

3

代替

<div class="display-label">Name</div>

<div class="display-label">@Html.LabelFor(model => model.Name)</div>

编辑 :

对于第二个问题,请看这里 我如何强制脚手架自动生成的代码在我的 asp.net mvc 3 的同一行显示 Label 和 EditorFor 文本字段(特别是最后一个答案)

于 2012-06-14T16:44:40.063 回答