我有一個簡單的例子。 兩班。用戶和公司如:MVC 5剃鬚刀 - 標籤沒有從課程註釋更新
public class User() {
public int UserID { get; set; }
[Display(Name = "User name")]
public string Name { get; set; }
[Display(Name = "Company")]
public int CompanyID { get; set; }
public virtual Company Company { get; set; }
}
public class Company() {
public int CompanyID { get; set; }
[Display(Name = "Company")]
public string Name { get; set; }
public virtual ICollection<User> Users { get; set; }
}
我的問題是在用戶的創建和編輯視圖。 「姓名」的標籤在「用戶名」中正確顯示,但「公司ID」的標籤保持顯示在「公司ID」下(所有公司都正確創建了下拉列表)。我希望標籤顯示「公司」,就像我在課堂上做的那樣。
我試圖改變我的觀點,但所有我做塊編譯,所以我迷路了。 我是MVC的begginer,所以請原諒,如果它很容易做,但我沒有看到它。
編輯(添加創建視圖代碼):
@model Intranet3.Models.User
@{
ViewBag.Title = "Add a user";
}
@using (Html.BeginForm(null, null, FormMethod.Post, htmlAttributes: new { @class = "form-horizontal form-bordered" })) {
@Html.AntiForgeryToken()
<div class="row-fluid">
<div class="span12">
<div class="box">
<div class="box-title">
<h3>
<i class="icon-table"></i>
New
</h3>
</div>
<div class="box-content nopadding">
<div class="form-horizontal">
@Html.MyValidationSummary()
<div class="control-group @Html.ClassErrorFor(model => model.Name)">
@Html.LabelFor(model => model.Name, new { @class = "control-label" })
<div class="controls">
@Html.EditorFor(model => model.Name)
@Html.MyValidationMessageFor(model => model.Name)
</div>
</div>
<div class="control-group @Html.ClassErrorFor(model => model.CompanyID)">
@Html.LabelFor(model => model.CompanyID, "CompanyID", new { @class = "control-label" })
<div class="controls">
@Html.DropDownList("CompanyID", String.Empty)
@Html.MyValidationMessageFor(model => model.CompanyID)
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Create</button>
<button onclick="location.href='@Url.Action("Index","User")'" type="button" class="btn">Cancel</button>
</div>
</div>
</div>
</div>
</div>
</div>
}
編輯2: 問題在標籤刪除字符串力解決。 所以這個:
@Html.LabelFor(model => model.CompanyID, "CompanyID", new { @class = "control-label" })
必須
@Html.LabelFor(model => model.CompanyID, new { @class = "control-label" })
請出示烏爾視圖代碼 –
的屬性利用就好了。嘗試「乾淨」和「重新構建」。讓我們知道問題是否仍然存在。並顯示您的查看代碼。 –
@NitinVarpe創建視圖已添加。多次清理和重建... – Saesee