我有这样的模式: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