MVC2 视图模型绑定

发布于 2024-09-14 06:42:49 字数 1226 浏览 3 评论 0原文

如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南巷近海 2024-09-21 06:42:49

您提到您正在使用 AutoMapper。在您的模型中,FullName 是一种方法,而不是属性。 AutoMapper 不会自动映射到方法。根据约定 您可以在方法名称前加上 Get 前缀以使其正常工作:

public string GetFullName()
{
    return string.Format("{0} {1}", FirstName, LastName);
}

这将映射到视图模型中的 FullName 属性。另一种选择是显式声明如何完成映射:

Mapper.CreateMap<Person, PersonViewModel>()
      .ForMember(
          dest => dest.FullName, 
          opt => opt.MapFrom(src => src.FullName())
      );

就部门名称属性而言,我建议您修改模型,以便它直接具有名为 的属性,而不是 DepartmentId 属性>Department 包含 id 和名称,这将允许您轻松地将它们映射到视图模型中。如果您无法以这种方式修改模型,则可以让视图模型中的 Department 属性直接由存储库而不是由 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 with Get to make this to work:

public string GetFullName()
{
    return string.Format("{0} {1}", FirstName, LastName);
}

This will be mapped to the FullName property in the view model. Another option is to explicitly declare how the mapping is done:

Mapper.CreateMap<Person, PersonViewModel>()
      .ForMember(
          dest => dest.FullName, 
          opt => opt.MapFrom(src => src.FullName())
      );

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 called Department 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 the Department property in the view model populated directly by the repository and not by AutoMapper.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文