好吧,有点卡在这里。
View 模型
public class UserProfileEdit
{
public virtual ApplicationUser ApplicationUser { get; set; }
[Required]
public string FirstName { get; set; }
public string TwitterHandle{ get; set; }
[Required]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
// etc etc
}
Controller
public ActionResult YourProfile()
{
string username = User.Identity.Name;
ApplicationUser user = db.Users.FirstOrDefault(u => u.UserName.Equals(username));
// Construct the viewmodel
UserProfileEdit model = new UserProfileEdit();
model.ApplicationUser = user;
return View(model);
}
在 View 上,我在顶部有@model MySite.Models.UserProfileEdit
。
如何将用户传递给 ViewModel?我知道我可以逐行完成
model.Email = user.Email;
举个例子,但是应该更简单吧?
最佳答案
您可以逐行进行,也可以使用 AutoMapper。尝试一下 http://automapper.org/
当您的代码中重复有相同类型的对象映射时,这非常有用。
关于c# - 将用户传递给 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37429014/