MVC2 视图模型绑定
如何将 BLL 中的属性传递给 ModeView。例如,我在单独的类库中拥有此类:
[MetadataType(typeof(PersonMetaData))]
public partial class Person
{
[Bind(Include = "PersonId,DepartmentId,FirstName,LastName,Active,DateAdded,DateDeleted")]
public class PersonMetaData
{
public object PersonId { get; set; }
public object DepartmentId { get; set; }
public object FirstName { get; set; }
public object LastName { get; set; }
public Department PersonDepartment { get; set; }
public string FullName()
{
return string.Format("{0} {1}", FirstName, LastName);
}
}
}
我的 ViewModel 如下所示:
public class PersonViewModel
{
public int PersonId { get; set; }
public string FullName{ get; set; }
public string PersonDepartment { get; set; }
}
当我生成强类型到 PersonViewModel 的新“视图”并设置为“列表”视图内容...时,会生成页面,但 FullName 没有通过。
我创建了 PersonDepartment 属性,因为我想显示人员所在的部门名称。我有一个类似设置的部门类。例如,我希望能够执行类似“PersonDepartment.DepartmentName”的操作,在页面上显示部门名称。
我正在使用 DBML (Linq To SQL),因此部分类是从自动生成的类扩展而来的。
我不确定如何填充 FullName 属性并将其传递给 ViewModel 以及如何获取连接到正在传递的人员信息的 Department 属性。任何帮助将不胜感激。
How do I get properties in my BLL passed down to a ModeView. For example, I have this class in a separate Class Library:
[MetadataType(typeof(PersonMetaData))]
public partial class Person
{
[Bind(Include = "PersonId,DepartmentId,FirstName,LastName,Active,DateAdded,DateDeleted")]
public class PersonMetaData
{
public object PersonId { get; set; }
public object DepartmentId { get; set; }
public object FirstName { get; set; }
public object LastName { get; set; }
public Department PersonDepartment { get; set; }
public string FullName()
{
return string.Format("{0} {1}", FirstName, LastName);
}
}
}
My ViewModel looks like this:
public class PersonViewModel
{
public int PersonId { get; set; }
public string FullName{ get; set; }
public string PersonDepartment { get; set; }
}
When I generate a new "View" strongly-typed to the PersonViewModel and set as "List" View Content....the page is generated, but FullName is not coming through.
I created the PersonDepartment property because I want to display the Department Name the person is in. I have a Department Class set up similarly. For example, I want to be able to do something like "PersonDepartment.DepartmentName" that displays the department name on the page.
I am using a DBML (Linq To SQL), so the partial classes are extending from the auto-generated classes.
I am not sure how to get FullName property filled and passed to ViewModel and get Department properties connected to the Person information being passed. Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提到您正在使用 AutoMapper。在您的模型中,
FullName
是一种方法,而不是属性。 AutoMapper 不会自动映射到方法。根据约定 您可以在方法名称前加上Get
前缀以使其正常工作:这将映射到视图模型中的
FullName
属性。另一种选择是显式声明如何完成映射:就部门名称属性而言,我建议您修改模型,以便它直接具有名为
的属性,而不是
包含 id 和名称,这将允许您轻松地将它们映射到视图模型中。如果您无法以这种方式修改模型,则可以让视图模型中的DepartmentId
属性>DepartmentDepartment
属性直接由存储库而不是由AutoMapper
填充。You have mentioned that you are using AutoMapper. In your model
FullName
is a method and not a property. AutoMapper won't map automatically to methods. According to the conventions you could prefix your method name withGet
to make this to work:This will be mapped to the
FullName
property in the view model. Another option is to explicitly declare how the mapping is done:As far as the department name property is concerned I would recommend you modify your model so that instead of a
DepartmentId
property it has directly a property calledDepartment
containing the id and the name which will allow you to map them easily in your view model. If you cannot modify your model this way you could have theDepartment
property in the view model populated directly by the repository and not byAutoMapper
.