即使我相信我已经定义了所有模型并希望正确传递,但我收到一个错误,指出当需要另一个模型时我正在传递一个模型。
主视图:
@model Project.Models.ProfileModel
@(ViewBag.HasLocalPassword ? Html.Partial("_ChangePasswordPartial", Model.PasswordModel) : Html.Partial("_SetPasswordPartial", Model.PasswordModel))
@Html.Partial("_UsernamePartial", Model.UsernameModel)
@Html.Partial("_PlayNamePartial", Model.PlayNameModel)
楷模:
public class ProfileModel
{
public PasswordModel PasswordModel { get; set; }
public PlayNameModel PlayNameModel { get; set; }
public UsernameModel UsernameModel { get; set; }
}
用户名操作 GET:
public ActionResult _UsernamePartial()
{
var usernameModel = new UsernameModel();
using (var db = new DataContext())
{
usernameModel.Username =
(from u in db.Users where u.ID == WebSecurity.CurrentUserId select u.Username).FirstOrDefault();
}
return PartialView(usernameModel);
}
用户名 POST 操作:
[HttpPost]
public ActionResult _UsernamePartial(UsernameModel usernameModel)
{
if (ModelState.IsValid)
{
using (var db = new DataContext())
{
User user = db.Users.FirstOrDefault(u => u.ID == WebSecurity.CurrentUserId);
user.Username = usernameModel.Username;
db.SaveChanges();
}
}
return View("_UsernamePartial");
}
配置文件操作:
public ActionResult Profile(ManageMessageId? message)
{
var profile = New ProfileModel();
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(profile);
}
用户名的查看页面:
@model Acatar.Models.UsernameModel
@using (Html.BeginForm("_UsernamePartial", "Account")) {
<p id="legend">Username</p>
@Html.TextBoxFor(m=>m.Username)
<button type="submit" value=" Username">Save</button>
}
错误信息: The model item passed into the dictionary is of type 'Project.Models.ProfileModel', but this dictionary requires a model item of type 'Project.Models.UsernameModel'.
非常感谢我哪里出错的任何帮助。