这是我的模型:
public class RestoreRequestModel : IDataErrorInfo
{
//true seems Value contains phone email otherwise
public bool IsPhoneMode { get; set; }
//Contains phone or email address.
public string Value { get; set; }
}
并查看:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>RestoreModel</legend>
<label for="email">By Email</label>
@Html.RadioButtonFor(x => x.IsPhoneMode, Constants.RESTORE_BY_EMAIL, new { id="email" })
<label for="phone">By Phone</label>
@Html.RadioButtonFor(x => x.IsPhoneMode, Constants.RESTORE_BY_PHONE, new { id = "phone" })
<div class="editor-label">
@Html.LabelFor(model => model.Value)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Value)
@Html.ValidationMessageFor(model => model.Value)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>}
是否可以根据 model.IsPhoneMode 状态设置验证?我可以通过向模型属性添加属性来设置验证,但必须有不同的验证条件,具体取决于 RadioButton 状态。