2个验证组

时间:2012-01-19 19:51:13

标签: asp.net-mvc-3 validation

我在页面上有2个验证表单。

<div id='LogOn' style="background-color: White;">
    @using (Ajax.BeginForm("LogOnAjax", "Home", new AjaxOptions { UpdateTargetId = "LogOn", OnSuccess = "logInComplete" }))
    { 
        //ITW2012Mobile.ViewModels.LogOnModel m = Model;
        @Html.HiddenFor(model => model.IsLoggedIn)
        @Html.HiddenFor(model => model.ReturnUrl)
        <div>
            @Html.ValidationSummary()
        </div>
        <div>
            @Html.LabelFor(model => model.UserName)
            @Html.EditorFor(model => model.UserName)
        </div>
        <div>
            @Html.LabelFor(model => model.Password)
            @Html.EditorFor(model => model.Password)
        </div>
        <div>
            <input type="submit" value="Login" />
        </div>
    }
</div>

<fieldset>
    @using (Html.BeginForm(Model.ActionMethod, Model.Controller))
    {
        @Html.ValidationSummary()
.....
            <div class="display-label"> E-mail Address * </div>
            <div class="display-field">
            @Html.EditorFor(model => model.Username)
            @Html.ValidationMessageFor(model => model.Username)
            </div>
            <div class="display-label"> Create Password * </div>
            <div class="display-field">
            @Html.PasswordFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
            </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    }
</fieldset>

我如何标记ModelState.AddModelError是表格的第一个,因为我在validation summary的两个地方都看到同样的错误?

1 个答案:

答案 0 :(得分:0)

  

如何标记ModelState.AddModelError用于表单的第一个

你做不到。

您可以做的是为每个表单属性而不是ValidationMessageFor

使用ValidationSummary