c# - 预填充表单时避免触发验证

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

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

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

我需要一些关于如何防止这种情况的指导。谢谢。

Controller

[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")
}

最佳答案

有人告诉我使用 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);
}

关于c# - 预填充表单时避免触发验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15657320/

相关文章:

c# - 如何从 UWP(又名 .NET Core)中的类型对象获取程序集

c# - 你怎么称呼循环进度条?

c# - NHibernate 无法匹配 DateTime 对象

c# - 如何在 Blazor 的 onkeypress 方法中读取输入的当前值?

c# - 启用 SSL 时 NopCommerce 商店内页未获取

asp.net-mvc - 在客户端清除outputcache

c# - 替换 C# 字符串中的多个字符

c# - 绑定(bind)到 ItemsControl ItemTemplate 内的 TextBox

asp.net 与 mysql

.net - 如何在 .net MVC 3 中使用角色测试授权属性?