美好的一天课程!
我正在管理帐户页面上,用户可以在其中更改密码和电子邮件。
我有一个管理视图,其中包含两个部分视图,更改密码和更改电子邮件。这两种都使用两种不同的模型。这样做的目的是在同一页面上同时包含更改密码和更改电子邮件。
如果当前密码正确,更改密码有效,但问题是当我输入错误的当前密码时。这给了我一个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";
}
<p class="message-success">@ViewBag.StatusMessage</p>
<p>Du är inloggad som <strong>@User.Identity.Name</strong>.</p>
<div id="Managebox1">
@Html.Partial("_ChangePasswordPartial")
</div>
<div id="Managebox2">
@Html.Partial("_ChangeEmailPartial")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
“子视图,更改密码”:
@model test.Models.LocalPasswordModel
<h3>Byt lösenord</h3>
@using (Html.BeginForm("Manage", "Account")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Change Password Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.OldPassword)
@Html.PasswordFor(m => m.OldPassword)
</li>
<li>
@Html.LabelFor(m => m.NewPassword)
@Html.PasswordFor(m => m.NewPassword)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
</ol>
<input type="submit" value="Byt lösenord" id="managesubmit" />
</fieldset>
}
“子视图,更改电子邮件”:
@model test.Models.LocalEmailModel
<h3>Byt E-postadress</h3>
@using (Html.BeginForm("ManageEmail", "Account")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Change Email Form</legend>
<ol>
<li>
@Html.LabelFor(n => n.OldEmail)
@Html.TextBoxFor(n => n.OldEmail)
</li>
<li>
@Html.LabelFor(n => n.NewEmail)
@Html.TextBoxFor(n => n.NewEmail)
</li>
<li>
@Html.LabelFor(n => n.ConfirmEmail)
@Html.TextBoxFor(n => n.ConfirmEmail)
</li>
</ol>
<input type="submit" value="Byt E-postadress" id="managesubmit"/>
</fieldset>
}
答案 0 :(得分:0)
当您使用单个参数(要渲染的局部视图的名称)调用Html.Partial
时,它会自动传入主视图的模型LocalPasswordModel
。您需要做的事情如下:
public class ManageViewModel
{
public ManageViewModel()
{
LocalPassword = new LocalPasswordModel();
LocalEmail = new LocalEmailModel();
}
public LocalPasswordModel LocalPassword { get; set; }
public LocalEmailModel LocalEmail { get; set; }
}
然后,在您的主视图中:
@model test.Models.ManageViewModel
...
<div id="Managebox1">
@Html.Partial("_ChangePasswordPartial", Model.LocalPassword)
</div>
<div id="Managebox2">
@Html.Partial("_ChangeEmailPartial", Model.LocalEmail)
</div>