预填表单时避免触发验证

时间:2013-03-27 11:06:04

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我已经创建了一个注册表单,该表单会预先填充一些字段显示给用户。那部分工作正常。

但是,我使用两种Register方法设置它的方式是在首次显示页面时触发表单验证,而不是仅在提交表单时触发。

我需要一些指导如何防止这种情况发生。感谢。

控制器

[HttpGet]
[AllowAnonymous]
public ActionResult Register(RegisterModel model, int? userID)
{
    if (userID != null && userID > 0)
    {
        int id = userID.Value;
        ExternalUser newUser = RepositoryHelper.GetExternalUserRepository().GetById(id);

        model.UserFirstName = newUser.NameFirst;
        model.UserLastName = newUser.NameLast;
    }
    return View(model);
}

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        try
        {
            IUnitOfWork unit = new EFUnitOfWork();
            var rep = new PersonRepository {UnitOfWork = unit};

            var entity = new Person
                {
                    NameTitle = model.NameTitle,
                    NameFirst = model.UserFirstName,
                    NameLast = model.UserLastName,
                    NameSalutation = model.NameSalutation,
                    DateOfBirth = model.DateOfBirth,
                };

            rep.Add(entity);
            unit.Commit();

            string confirmationToken = WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { entity.Id, IsApproved = model.IsApproved =false }, true);
            WebSecurity.Login(model.UserName, model.Password);

            return RedirectToAction("RegisterStepTwo", "Account");
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

视图

@model Models.RegisterModel

<div class="content">
    <hgroup class="title">
        <h1>@ViewBag.Title.</h1>
    </hgroup>

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary()

        <fieldset>
            <legend>Registration Form</legend>
            <div id="container">
                <div class="firstContentColumn">
                    <section>
                        <ol>
                            <li>
                                @Html.LabelFor(m => m.UserFirstName)
                                @Html.TextBoxFor(m => m.UserFirstName)
                                <p>@Html.ValidationMessageFor(m => m.UserFirstName)</p>
                            </li>
                            <li>
                                @Html.LabelFor(m => m.UserLastName)
                                @Html.TextBoxFor(m => m.UserLastName)
                                <p>@Html.ValidationMessageFor(m => m.UserLastName)</p>
                            </li>
                        </ol>
                    </section>
                </div>

                        <input type="submit" value="Register" />
                    </section>
                </div>

            </div>
        </fieldset>  
    }
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jqueryui")
    @Styles.Render("~/Content/themes/base/css")
}

1 个答案:

答案 0 :(得分:0)

我被告知使用ModelState.Clear会阻止初步验证,而且似乎工作正常。

[HttpGet]
[AllowAnonymous]
public ActionResult Register(RegisterModel model, int? userID)
{
ModelState.Clear();

    if (userID != null && userID > 0)
    {
        int id = userID.Value;
        ExternalUser newUser = RepositoryHelper.GetExternalUserRepository().GetById(id);

        model.UserFirstName = newUser.NameFirst;
        model.UserLastName = newUser.NameLast;
    }
    return View(model);
}