C# 避免在预填充表单时触发验证

C# 避免在预填充表单时触发验证,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我已经创建了一个注册表,它向用户显示一些预先填充的字段。那部分很好用 但是,我使用两个Register方法进行设置的方式是,在第一次显示页面时触发表单验证,而不是仅在提交表单时触发 我需要一些关于如何防止这种情况的指导。谢谢 控制器 看法 @model Models.RegisterModel @ViewBag.Title。 @使用(Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary() 登记表 @Labe

我已经创建了一个注册表,它向用户显示一些预先填充的字段。那部分很好用

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

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

控制器

看法
@model Models.RegisterModel
@ViewBag.Title。
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
登记表
  • @LabelFor(m=>m.UserFirstName) @Html.TextBoxFor(m=>m.UserFirstName) @Html.ValidationMessageFor(m=>m.UserFirstName)

  • @LabelFor(m=>m.UserLastName) @Html.TextBoxFor(m=>m.UserLastName) @Html.ValidationMessageFor(m=>m.UserLastName)

  • } @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) @Scripts.Render(“~/bundles/jqueryui”) @style.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);
    }
    

    你能发表你的观点吗?奇怪,一切看起来都很好。你在页面加载时没有任何额外的jquery调用
    。validate
    对吗?不,没有额外的调用
    。validate
    奇怪的是,如果你中断了
    HttpPost
    ,加载时会命中吗?不,加载时不会命中。谢谢你抽出时间,马蒂。
    [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")
    }
    
    [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);
    }