我有這樣的模式:MVC 3編輯觀點僅
public class ExchangeRate
{
[Key]
public int ExchangeRateID { get; set; }
[Required]
[Display(Name = "Currency:")]
public string Currency { get; set; }
[Required]
public decimal Rate { get; set; }
}
「創建」視圖工作正常,但是當我在編輯看來,我只希望貨幣屬性顯示,而不是可編輯的。我應該怎麼做?如果我爲這個類創建另一個「僅供查看」模型,那麼我將省略「Currency」屬性,並且將無法顯示它。
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>ExchangeRate</legend>
@Html.HiddenFor(model => model.ExchangeRateID)
<div class="editor-label">
@Html.LabelFor(model => model.Currency)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.Currency)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Rate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Rate)
@Html.ValidationMessageFor(model => model.Rate)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
更改@ Html.EditorFor(型號=> model.Currency)至@ Html.DisplayFor(型號=> model.Currency)不起作用,因爲模型的狀態變爲無效時,它回發給控制器。
這也可以與Twitter引導結合使用,例如,在視圖中使用下面的代碼:' @ Html.DisplayFor(m => m.ProductName)' – Manfred 2012-08-28 01:07:17
用戶可以不通過螢火蟲等編輯read-olny字段嗎? – Sami 2013-10-04 09:34:35