美好的一天!
我正在管理帐户页面上,用户可以在其中更改密码和电子邮件 .
我有一个管理视图,其中有两个部分视图,更改密码和更改电子邮件 . 这两种都使用两种不同的模型 . 这样做的目的是在同一页面上同时包含更改密码和更改电子邮件 .
如果当前密码正确,则更改密码有效,但问题是当我输入错误的当前密码时 . 这给了我一个InvalidOperationException:
“传递到字典中的模型项的类型为'test.Models.LocalPasswordModel',但此字典需要类型为'test.Models.LocalEmailModel'的模型项 . ”
忽略瑞典语错误消息:)
这是我的模特:
public class LocalPasswordModel
{
[Required(ErrorMessage = "Nuvarande lösenord måste fyllas i.")]
[DataType(DataType.Password)]
[Display(Name = "Nuvarande lösenord")]
public string OldPassword { get; set; }
[Required(ErrorMessage = "Nya lösenordet måste fyllas i.")]
[StringLength(100, ErrorMessage = "Lösenordet måste vara minst 4 tecken lång.", MinimumLength = 4)]
[DataType(DataType.Password)]
[Display(Name = "Nya lösenordet")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Bekräfta nya lösenordet")]
[Compare("NewPassword", ErrorMessage = "De nya angivna lösenorden stämmer inte med varandra.")]
public string ConfirmPassword { get; set; }
}
public class LocalEmailModel
{
[Required(ErrorMessage = "Nuvarande E-postadress måste fyllas i.")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Nuvarande E-postadress")]
public string OldEmail { get; set; }
[Required(ErrorMessage = "Nya E-postadressen måste fyllas i.")]
[System.Web.Mvc.Remote("IsUserEmailAvailable", "Account", ErrorMessage = "E-postadressen används redan. Var god skriv in en annan E-postadress.")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Nya E-postadressen")]
public string NewEmail { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name = "Bekräfta nya E-postadressen")]
[Compare("NewEmail", ErrorMessage = "De nya angivna E-postadresserna stämmer inte med varandra.")]
public string ConfirmEmail { get; set; }
}
这是我的看法:
“家长视图”:
@model test.Models.LocalPasswordModel
@{
ViewBag.Title = "Hantera ditt konto";
}
Du är inloggad som @User.Identity.Name.
@Html.Partial("_ChangePasswordPartial")
@Html.Partial("_ChangeEmailPartial")
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
“子视图,更改密码”:
@model test.Models.LocalPasswordModel
Byt lösenord
@using (Html.BeginForm("Manage", "Account")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
Change Password Form
@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)
}
“儿童视图,更改电子邮件”:
@model test.Models.LocalEmailModel
Byt E-postadress
@using (Html.BeginForm("ManageEmail", "Account")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
Change Email Form
@Html.LabelFor(n => n.OldEmail)
@Html.TextBoxFor(n => n.OldEmail)
@Html.LabelFor(n => n.NewEmail)
@Html.TextBoxFor(n => n.NewEmail)
@Html.LabelFor(n => n.ConfirmEmail)
@Html.TextBoxFor(n => n.ConfirmEmail)
}