【发布时间】:2013-02-13 18:15:37
【问题描述】:
我有一个视图页面,该页面具有不同模型的不同部分视图。我创建了一个模型类,它将调用其他类,因此我可以在主视图页面上使用它。但我的问题是,当我尝试更改密码时,它给了我一个错误,我正在传递一个模型,我需要传递另一个模型。我相信我的结构是正确的,但不确定是什么导致了这个问题。
主视图:
@model Acatar.Models.ProfileModel
@{
ViewBag.Title = "ProfileAccount";
}
@{ Html.RenderAction("_PlayNamePartial"); }
@{ Html.RenderAction("_UsernamePartial", "Account");}
@{ Html.RenderAction("_TalentsPartial", "Account");}
@if (ViewBag.HasLocalPassword)
{
@Html.Partial("_ChangePasswordPartial")
}
else
{
@Html.Partial("_SetPasswordPartial")
}
Profile Model:包含我创建的模型
public class ProfileModel
{
public LocalPasswordModel LocalPasswordModel { get; set; }
public PlayNameModel PlayNameModel { get; set; }
public UsernameModel UsernameModel { get; set; }
public TalentsModel TalentsModel { get; set; }
}
控制器:
public ActionResult Profile(ManageMessageId? message)
{
ViewBag.StatusMessage =
message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
: message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
: message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed."
: "";
ViewBag.HasLocalPassword = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
ViewBag.ReturnUrl = Url.Action("Profile");
return View();
}
发布:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Profile(LocalPasswordModel model)
{
bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
ViewBag.HasLocalPassword = hasLocalAccount;
ViewBag.ReturnUrl = Url.Action("Profile");
if (hasLocalAccount)
{
if (ModelState.IsValid)
{
// ChangePassword will throw an exception rather than return false in certain failure scenarios.
bool changePasswordSucceeded;
try
{
changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword);
}
catch (Exception)
{
changePasswordSucceeded = false;
}
if (changePasswordSucceeded)
{
return RedirectToAction("Profile", new { Message = ManageMessageId.ChangePasswordSuccess });
}
else
{
ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
}
}
}
else
{
// User does not have a local password so remove any validation errors caused by a missing
// OldPassword field
ModelState state = ModelState["OldPassword"];
if (state != null)
{
state.Errors.Clear();
}
if (ModelState.IsValid)
{
try
{
WebSecurity.CreateAccount(User.Identity.Name, model.NewPassword);
return RedirectToAction("Profile", new { Message = ManageMessageId.SetPasswordSuccess });
}
catch (Exception e)
{
ModelState.AddModelError("", e);
}
}
}
return View(model);
}
查看密码修改页面:
@model Project.Models.LocalPasswordModel
@using (Html.BeginForm("Profile", "Account")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Change Password Form</legend>
@Html.LabelFor(m => m.OldPassword)
@Html.PasswordFor(m => m.OldPassword)
@Html.LabelFor(m => m.NewPassword)
@Html.PasswordFor(m => m.NewPassword)
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
<br/>
<input class="btn btn-small" type="submit" value="Change password" />
</fieldset>
我得到的错误:
The model item passed into the dictionary is of type 'Project.Models.LocalPasswordModel', but this dictionary requires a model item of type 'Project.Models.ProfileModel'.
【问题讨论】:
-
你在哪里传递你的个人资料模型?
-
@ssilas777 在主视图页面上
-
但是从你的控制器你没有传递任何东西 - View();您应该填充您的配置文件模型并像视图(模型)一样传递它。所有其他部分视图都使用 Renderaction,因此不会出错,但在 @Html.Partial 中它需要模型。跳它是有道理的
-
@ssilas777 所以你认为在我的
...Profile(ManageMessageId? message)中我应该包含var model = new ProfileModel();然后在返回中:return View(model)吗? -
是的,你也应该填充 model.LocalPasswordModel 模型,因为我们将在我们的视图中使用它。否则,您可以考虑将 @Html.Partial 更改为 Html.RenderAction,就像对其他视图所做的那样。
标签: asp.net-mvc-4 model viewmodel asp.net-mvc-viewmodel