gpt4 book ai didi

c# - 由于 PartialView 后 ModelState 丢失,MVC 3 验证消息未显示

转载 作者:太空狗 更新时间:2023-10-30 01:24:26 32 4
gpt4 key购买 nike

我有一个 MVC 3 项目,其中有 1 个 View LoginRegister,其中包含 2 个带有登录和预注册表单的 View 。问题是在错误地完成预注册表格并使用 PartialView("LoginRegister", loginRegisterViewModel) 验证消息后由于 ModelState 丢失而未显示。在阅读下一段之前,最好跳转到 CODE

调试 PartialView("LoginRegister", loginRegisterViewModel) 并进入到以下 @Html.ErrorMessageFor(model => model.Email) 的 PreRegister View 。其中 ModelState 不包含 Email 键(见下文),因此不显示错误消息。

    private static MvcHtmlString ErrorMessageHelper(this HtmlHelper htmlHelper, ModelMetadata modelMetadata, string expression, string validationMessage, IDictionary<string, object> htmlAttributes)
{
string modelName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression);

if (!htmlHelper.ViewData.ModelState.ContainsKey(modelName)) // ModelState contains no keys, e.g. Email not found and a null is returned
{
return null;
}

// code continues here to render error message
}

代码

View 模型

登录注册 View 模型

namespace Site.ViewModels.Account
{
public class LoginRegisterViewModel
{
public bool IsDialog { get; set; }
public PreRegisterViewModel PreRegister { get; set; }
public QuickLoginViewModel QuickLogin { get; set; }
}
}

PreRegisterViewModel

using FluentValidation.Attributes;
using Site.ViewModels.Validators;

namespace Site.ViewModels.Account
{
[Validator(typeof(PreRegisterViewModelValidator))]
public class PreRegisterViewModel
{
public string Email { get; set; }
public string ConfirmEmail { get; set; }
}
}

观看次数

登录注册

@model Site.ViewModels.Account.LoginRegisterViewModel

@{
Layout = ViewBag.Layout;
}

<div id="loginReg" class="mod-loginReg">
<h2>Login and Registration</h2>
@{Html.RenderPartial("PreRegister", Model.PreRegister, new ViewDataDictionary());}
@{Html.RenderPartial("QuickLogin", Model.QuickLogin, new ViewDataDictionary());}
</div>

预注册

@using Site.Classes.MVC.Extensions;

@model Site.ViewModels.Account.PreRegisterViewModel

@using (Html.BeginForm("PreRegister", "Account", FormMethod.Post, new { @class = "form-errorContainer" }))
{
<div class="mod-loginReg-register">
<h3>Register</h3>
<p>Please enter your email address to register.<br /><br /></p>

<div class="mod-loginReg-form">
<div class="field-item">
@Html.LabelFor(model => model.Email)
@Html.TextBoxFor(model => model.Email, new { @maxlength = "255", @class = "Textbox required email" })
@Html.ErrorMessageFor(model => model.Email)
</div>
<div class="field-item">
@Html.LabelFor(model => model.ConfirmEmail)
@Html.TextBoxFor(model => model.ConfirmEmail, new { @maxlength = "255", @class = "Textbox required email" })
@Html.ErrorMessageFor(model => model.ConfirmEmail)
</div>
<div class="button-group">
@Html.Button("Register", "Register")
</div>
</div>
</div>
}

账户 Controller

    [RequireHttps]
public ActionResult LoginRegister(int showDialog)
{
var loginRegisterViewModel = new LoginRegisterViewModel();

if (showDialog == 1)
{
ViewBag.Layout = "~/layouts/structure/dialog.cshtml";
loginRegisterViewModel.IsDialog = true;
}
else
{
ViewBag.Layout = "~/layouts/structure/2column.cshtml";
}

return PartialView(loginRegisterViewModel);
}

[RequireHttps]
public ActionResult PreRegister()
{
return PartialView();
}

[HttpPost]
[RequireHttps]
public ActionResult PreRegister(PreRegisterViewModel model)
{
if (ModelState.IsValid)
{
return PartialView("PreRegisterComplete");
}

var loginRegisterViewModel = new LoginRegisterViewModel { PreRegister = model, QuickLogin = new QuickLoginViewModel() };

return PartialView("LoginRegister", loginRegisterViewModel);
}

最佳答案

这是由于 viewData 在调用 new ViewDataDictionary() 时被清除造成的。

@{Html.RenderPartial("PreRegister", Model.PreRegister, new ViewDataDictionary());}
@{Html.RenderPartial("QuickLogin", Model.QuickLogin, new ViewDataDictionary());}

根据 MSDN ViewDataDictionary 用于:

Represents a container that is used to pass data between a controller and a view.

我假设您对此有充分的理由,否则您不会不厌其烦地添加附加参数。

如果将其更改为:

@Html.RenderPartial("PreRegister", Model.PreRegister)
@Html.RenderPartial("QuickLogin", Model.QuickLogin)

在 Controller 中构建的 ModelState 应该在您的 View 中可用。

关于c# - 由于 PartialView 后 ModelState 丢失,MVC 3 验证消息未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9348897/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com