Asp.net 尽管消息已添加到ModelState,但Html.ValidationMessage为空

Asp.net 尽管消息已添加到ModelState,但Html.ValidationMessage为空,asp.net,asp.net-mvc,validation,asp.net-mvc-4,view,Asp.net,Asp.net Mvc,Validation,Asp.net Mvc 4,View,我试图同时使用ModelState.adderRorModel和Html.ValidationMessage(“帐户”)来显示错误消息 但是,即使我可以调试文章中添加的模型错误,当再次加载视图时,我也无法在视图中看到它 代码在表单的POST上运行,我可以看到调用了ModelState.addmodeleror(例如,Message也有一个值): 我的观点是这样的: <h3>Get your free account now</h3> @using (Ht

我试图同时使用
ModelState.adderRorModel
Html.ValidationMessage(“帐户”)
来显示错误消息

但是,即使我可以调试文章中添加的模型错误,当再次加载视图时,我也无法在视图中看到它

代码在表单的POST上运行,我可以看到调用了ModelState.addmodeleror(例如,Message也有一个值):

我的观点是这样的:

 <h3>Get your free account now</h3>
        @using (Html.BeginForm("Register", "Home", FormMethod.Post, new { @class = "form" }))
        {
            // trying to test different methods to get my errors... NOTHING works :)
            @Html.ValidationSummary()
            @Html.ValidationMessage("Register")
            @Html.BusinessErrorMessageBox()


            <div class="form-group">
                <label>Email address</label>
                @Html.TextBoxFor(m => m.RegisterViewModel.Email, new { @class = "form-control", placeholder = "Email", @type = "email" })
                @Html.ValidationMessageFor(m => m.RegisterViewModel.Email)
            </div>
            <div class="form-group">
                <label>Password</label>
                @Html.PasswordFor(m => m.RegisterViewModel.Password, new { @class = "form-control", placeholder = "Password" })
                @Html.ValidationMessageFor(m => m.RegisterViewModel.Password)
            </div>
            <div class="form-group">
                <label>Your country (the country where you live or where your organization is registered in)</label>
                @Html.DropDownListFor(model => model.RegisterViewModel.SelectedCountry, Model.RegisterViewModel.Countries, new { @class = "form-control" })
            </div>
            <input type="submit" class="btn btn-primary btn-lg" value="Get your free account now" />
        }
try {
    //your logic here

    //Everything worked, return a redirect
    return RedirectToAction("Account", accountViewModel);
}
catch (Exception ex)
{
    logger.Error(ex);
    ModelState.AddModelError("Register",ex.Message);
}

//If we get here, something went wrong. Return the view so errors are displayed
return View("Account", accountViewModel);

实际上,你想要的是:

@Html.ValidationSummary(false)

您需要表单上显示的所有错误:。由于没有将模型错误附加到特定属性,因此在任何表单元素验证中都不会显示该错误

POST操作的结果始终是重定向:

return RedirectToAction("Account", accountViewModel);
这意味着您的错误消息将丢失,因为重定向将告诉浏览器为
帐户
操作发送另一个GET请求,在该操作中,您将呈现一个全新的注册视图,并且没有错误

您想要的流程是:

@Html.ValidationSummary(false)
  • POST逻辑成功后,返回重定向到
    帐户
    ,该帐户将发送GET请求
  • POST逻辑失败,请返回相同的视图,以便显示错误
您的POST方法如下所示:

 <h3>Get your free account now</h3>
        @using (Html.BeginForm("Register", "Home", FormMethod.Post, new { @class = "form" }))
        {
            // trying to test different methods to get my errors... NOTHING works :)
            @Html.ValidationSummary()
            @Html.ValidationMessage("Register")
            @Html.BusinessErrorMessageBox()


            <div class="form-group">
                <label>Email address</label>
                @Html.TextBoxFor(m => m.RegisterViewModel.Email, new { @class = "form-control", placeholder = "Email", @type = "email" })
                @Html.ValidationMessageFor(m => m.RegisterViewModel.Email)
            </div>
            <div class="form-group">
                <label>Password</label>
                @Html.PasswordFor(m => m.RegisterViewModel.Password, new { @class = "form-control", placeholder = "Password" })
                @Html.ValidationMessageFor(m => m.RegisterViewModel.Password)
            </div>
            <div class="form-group">
                <label>Your country (the country where you live or where your organization is registered in)</label>
                @Html.DropDownListFor(model => model.RegisterViewModel.SelectedCountry, Model.RegisterViewModel.Countries, new { @class = "form-control" })
            </div>
            <input type="submit" class="btn btn-primary btn-lg" value="Get your free account now" />
        }
try {
    //your logic here

    //Everything worked, return a redirect
    return RedirectToAction("Account", accountViewModel);
}
catch (Exception ex)
{
    logger.Error(ex);
    ModelState.AddModelError("Register",ex.Message);
}

//If we get here, something went wrong. Return the view so errors are displayed
return View("Account", accountViewModel);

希望有帮助

您是否尝试过
ModelState.addmodeleror(string.Empty,ex.Message)@Html.ValidationSummary(true)
进行编码。不幸的是,这没有帮助。仅供参考-页面上还有2个表单不幸的是,这并没有改变视图中的任何内容:-(感谢您的尝试,这非常奇怪。如果您在页面上创建了一个视图源,然后查看表单下方,是否显示了任何错误?您现在可以看到HTML输出:-)这应该是正确的。将模型返回到视图而不重定向应该非常有效。非常感谢你的回答,这解决了我的问题。我会在可能的时候奖励赏金(19小时!)