3

我创建了一个向用户显示的注册表单,其中预先填充了一些字段。那部分工作正常。

但是,我使用两个 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")
}
4

1 回答 1

0

我被告知 usingModelState.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);
}
于 2013-03-27T14:20:49.017 回答