请忍受我的noobness,我对 MVC 模式非常陌生。
我正在尝试做什么
我正在为我的网站上的注册用户构建个人资料信息页面。该页面将列出有关用户的数据,例如出生日期、电话号码、订阅状态等。您明白了。我还想要一个表格让用户更改密码、电子邮件地址、个人信息在同一页上.
我的问题
用户的数据通过传递的模型变量来自我的控制器:
public ActionResult Profil()
{
var model = db.Users.First(e => e.UserName == WebSecurity.CurrentUserName);
return View(model);
}
在我看来,输出如下:
<label>Phone number: </label>
@if (Model.PhoneNumber != null)
{
@Model.PhoneNumber
}
else
{
<span class="red">You haven't set up your phone number yet. </span>
}
用户可以更改其信息的形式将使用另一个模型,ProfileModel。因此,在我看来,基本上我需要使用两种模型,一种用于输出信息,一种用于发布数据。我认为使用部分视图可以实现此目的,但出现此错误:
传入字典的模型项的类型为
'Applicense.Models.User',但是该字典需要一个模型项
输入“Applicense.Models.ProfileModel”。
这是我对部分视图的调用如下所示:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
@Html.Partial("_ModifyProfileInfo")
}
这是部分视图:
@model Applicense.Models.ProfileModel
<ul>
<li>
@Html.LabelFor(m => m.Email)
@Html.EditorFor(m => m.Email)
</li>
<li>
@Html.LabelFor(m => m.ConfirmEmail)
@Html.EditorFor(m => m.ConfirmEmail)
</li>
<input type="submit" value="Update e-mail" />
</ul>
最后这是我的 ProfileModel:
public class ProfileModel
{
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "New e-mail address")]
public string Email { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name = "Confirm new e-mail address")]
[Compare("Email", ErrorMessage = "The e-mail and it's confirmation field do not match.")]
public string ConfirmEmail { get; set; }
}
我错过了什么吗?执行此操作的正确方法是什么?
Edit:我重新编写了代码以反映 Nikola Mitev 的答案,但现在我遇到了另一个问题。这是我得到的错误:
你调用的对象是空的。 (@Model.UserObject.LastName)
仅当我发布更改后的电子邮件地址值时才会发生这种情况。这是我的 ViewModel(配置文件 Model.cs):
public class ProfileModel
{
public User UserObject { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Új e-mail cím")]
public string Email { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name = "Új e-mail cím megerősítése")]
[Compare("Email", ErrorMessage = "A két e-mail cím nem egyezik.")]
public string ConfirmEmail { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name= "E-mail cím")]
public string ReferEmail { get; set; }
}
控制器:
public ActionResult Profil()
{
var User = db.Users.First(e => e.UserName == WebSecurity.CurrentUserName);
var ProfileViewModel = new ProfileModel
{
UserObject = User
};
return View(ProfileViewModel);
}
最后这是我的user.cs
模型类:
[Table("UserProfile")]
public class User
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[Column("UserName")]
public string UserName { get; set; }
[Column("Email")]
[Required]
public string Email { get; set; }
[Column("FirstName")]
public string FirstName { get; set; }
[Column("LastName")]
public string LastName { get; set; }
[Column("PhoneNumber")]
public string PhoneNumber { get; set; }
... You get the idea of the rest...
我认为这是因为模型试图将数据放入每个required
列存入数据库。
Edit2:我的 Profile 操作的 httppost 方法:
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult Profil(ProfileModel model)
{
if (ModelState.IsValid)
{
//insert into database
return Content("everything's good");
}
else
{
//outputs form errors
return View(model);
}
}