【问题标题】:Properly handling model validation errors正确处理模型验证错误
【发布时间】:2016-05-27 13:27:59
【问题描述】:

如何进行这个简单的表单验证?

我有一个 AccountVerificationController,它最初具有以下方法:

public ActionResult Index(AccountVerificationModel model)

问题是最初加载视图时,由于模型具有以下必填字段,因此存在验证错误:

 public class AccountVerificationModel
    {
        [Required]        
        public string MerchantId { get; set; }
        [Required]       
        public string AccountNumber { get; set; }
        [Required, StringLength(9, MinimumLength = 9, ErrorMessage = "The Routing Number must be 9 digits")]  
}

但是,这不是我们想要的行为。我希望仅在用户单击验证按钮后进行验证,因此我更改了表单以调用帐户控制器中的验证方法。

视图如下;

@using (Html.BeginForm("Verify", "AccountVerification", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h2>@ViewBag.Title</h2>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.SubMerchantId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.MerchantId, new { htmlAttributes = new { @class = "form-control", placeholder = "MID" } })
                @Html.ValidationMessageFor(model => model.MerchantId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.RoutingNumber, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.RoutingNumber, new { htmlAttributes = new { @class = "form-control", placeholder = "9 Digit Routing Number" } })
                @Html.ValidationMessageFor(model => model.RoutingNumber, "", new { @class = "text-danger" })
            </div>
        </div>           

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input name="validate" type="submit" value="Validate" class="btn btn-info"/>
            </div>
        </div>
    </div>
}

现在的挑战是处理模型验证错误。我的控制器结构如下:

  public class AccountVerificationController : BaseMvcController
{    

    public AccountVerificationController()
    {
    }    

      public ActionResult Index()
        {            
            return View(new AccountVerificationModel());
        }

    public ActionResult Verify(AccountVerificationModel model)
    {
        // do the validation then re-direct......
        if (!model.IsValid())
        {
            return RedirectToAction("Index", model);
        }

        // otherwise try to validate the account

        if (!model.VerificationSuccessful)
        {
            // repopulate the view with this model...
            return RedirectToAction("Index", model);
        }

        return Redirect("Index");           
    }

但是,在重定向期间,我丢失了整个上下文、模型错误和所有内容。阅读整个模型绑定,但如果有人能很快发现我在这里做错了什么,那将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    好吧,当出现错误时,您无法重定向。时期。重定向实际上是一个两步过程,尽管它似乎是无缝发生的。首先,服务器返回一个 301 或 302 状态代码,其中的标头值指示客户端应该转到的 URL。然后,客户端向该给定 URL 发出一个全新的请求。这就是为什么没有保留上下文的原因。与客户端第一次请求页面基本相同。

    维护上下文的唯一方法是简单地返回一个视图。这意味着您的 Verify 操作需要返回与您的 Index 操作相同的视图。然而,这并不完全理想。我不完全理解最初的问题是什么让您决定首先添加 Verify 操作,但如果您对此提出新问题,则可能会解决此问题。您真的应该回发到您的 Index 操作。

    【讨论】:

    • 最初的问题是表单在首次打开时显示验证错误。我将发布一个新问题。谢谢。
    • 我添加了验证操作,因此它被表单发布调用。
    猜你喜欢
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    相关资源
    最近更新 更多