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

FoolProof検証パッケージをNugetします。このような状況で完全に機能します。

于 2012-06-21T17:49:40.007 に答える