【发布时间】:2015-03-19 06:43:34
【问题描述】:
好吧,我正在尝试将我的用户配置文件模型映射到用户配置文件视图模型/从用户配置文件视图模型映射,但由于它们不共享所有数据字段,因此它不起作用:
型号:
public class UserProfile
{
public virtual string Id { get; set; }
public virtual string Interests { get; set; }
public virtual string Biography { get; set; }
public virtual string SmokingAttitude { get; set; }
public virtual string DrinkingAttitude { get; set; }
public virtual string Projects { get; set; }
public virtual string Groups { get; set; }
}
视图模型:
public class UserProfileViewModel
{
[Display(Name = "Interests")]
public string Interests { get; set; }
[Display(Name = "Biography/Comments")]
public string Biography { get; set; }
[Display(Name = "Attitude to Smoking")]
public string SmokingAttitude { get; set; }
[Display(Name = "Attitude to Drinking")]
public string DrinkingAttitude { get; set; }
[Display(Name = "Projects working on")]
public string Projects { get; set; }
[Display(Name = "Groups joined")]
public string Groups { get; set; }
public IEnumerable<SelectListItem> AttitudeList
{
get
{
return new[]{
new SelectListItem {Value = "not specified", Text = "not specified"},
new SelectListItem {Value = "very negative", Text = "very negative"},
new SelectListItem {Value = "negative", Text = "negative"},
new SelectListItem {Value = "compromisable", Text = "compromisable"},
new SelectListItem {Value = "neutral", Text = "neutral"},
new SelectListItem {Value = "positive", Text = "positive"},
};
}
}
}
如您所见,ID 属性存储在用户配置文件模型中(作为用户表的键),而不是用户配置文件视图模型。但是,用户配置文件视图模型具有为选择/下拉列表操作定义的附加字段 AttitudeList。我认为这是 automapper 无法正常工作的原因,但我完全不知道如何解决这个问题。
有人可以帮忙吗?这个想法只是将公共字段从模型映射到视图模型/从视图模型映射,同时忽略不同的字段。我希望我不必只是手动映射它们编写数十行代码...
【问题讨论】:
标签: c# asp.net-mvc model-view-controller asp.net-mvc-5 automapper