【发布时间】:2017-03-15 13:38:23
【问题描述】:
我有这个模型:
public class ExchangeRate
{
[Key]
public int ExchangeRateID { get; set; }
[Required]
[Display(Name = "Currency:")]
public string Currency { get; set; }
[Required]
public decimal Rate { get; set; }
}
“创建”视图工作正常,但是当我在编辑视图中时,我只希望显示货币属性,而不是可编辑的。我该怎么做?如果我为这个类创建另一个“仅查看”模型,那么我将省略“货币”属性并且无法显示它。
@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 => model.Currency) 更改为@Html.DisplayFor(model => model.Currency) 不起作用,因为模型状态在回发到控制器时变得无效。
【问题讨论】:
标签: asp.net-mvc-3