0

这是我的模型:

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 状态。

4

2 回答 2

0

你需要的是实现你自己的属性,这个属性是有条件的,你可以搜索很多关于该主题的博客文章,或者你可以下载Mvc.ValidationTookit

于 2012-06-21T17:48:15.303 回答
0

Nuget FoolProof 验证包。它完全适用于这种情况。

于 2012-06-21T17:49:40.007 回答