Automapper映射的IEnumerable< SelectListItem>对于下拉菜单 [英] Automapper Mapping IEnumerable<SelectListItem> for Dropdown Menu

查看:291
本文介绍了Automapper映射的IEnumerable< SelectListItem>对于下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我目前将自动映射到我的MVC项目,我卡住了。现在我已经用在数据库中重新present数据的用户模型。我有该模型映射到时编辑方法被称为将用于一个EditUserModel。该EditUserModel有的IEnumerable< SelectListItem方式> (对于下拉菜单),我似乎无法弄清楚如何映射

尝试性解决方案

截至目前我还没有尝试过实施什么。我不确定其中用于最好的地方了IEnumerable< SelectListItem> 或者填充它。现在它正在被填充在控制器中。

User.cs

 公共类用户
{
    [键]
    公众诠释用户名{搞定;组; }    公共字符串用户名{获得;组; }    公共字符串密码{搞定;组; }    公众诠释角色ID {搞定;组; }    [ForeignKey的(角色ID)]
    公共虚拟角色角色{搞定;组; }
}

EditUserModel.cs

 公共类EditUserViewModel
{
    [HiddenInput(DisplayValue = FALSE)]
    公众诠释用户名{搞定;组; }    [需要]
    公共字符串用户名{搞定;组; }    [需要]
    [数据类型(DataType.Password)
    公共字符串密码{搞定;组; }    [显示名称(角色)]
    [需要]
    公众诠释角色ID {搞定;组; }    //麻烦场
    公共IEnumerable的< SelectListItem>角色{搞定;组; }
}

Controller.cs

  EditUserViewModel模式=新EditUserViewModel();
下拉菜单中的人口//
model.Roles = context.Roles
    .ToList()
    。选择(X =>新建SelectListItem
    {
        文= x.Description,
        值= x.RoleID.ToString()
    });
//映射的automaper会照顾
model.UserID = user.UserID;
model.Username = user.Username;
model.RoleID = user.RoleID;


解决方案

有关的记载,这里是我在评论中谈论到的Jakub的回答是:

 公共静态类EnumerableExtensions
{
    公共静态的IEnumerable< SelectListItem> ToSelectList< T,TTextProperty,TValueProperty>(这个IEnumerable的< T>例如,函数功能< T,TTextProperty>文字,Func键< T,TValueProperty>的价值,Func键< T,BOOL>将selectedItem = NULL)
    {
        返回instance.Select(T =>新建SelectListItem
        {
            文字= Convert.ToString(文字(T)),
            值= Convert.ToString(值(t))的
            选择=将selectedItem!= NULL?将selectedItem(T):假
        });
    }
}

不用说,这是显着地更简单和完成同样的事情(实际上是在事件更健壮的属性路径是非简单,因为的Jakub的溶液不会与嵌套属性工作)。

(这是不是一个真正的答案,我张贴它作为一个社区维基只是帮助制定一个点)

Problem

I am currently adding automapping to my MVC project and I am stuck. Right now I have a User model used to represent data in the database. I have to map that model to a EditUserModel which will be used when the Edit method is called. The EditUserModel has IEnumerable<SelectListItem> (for a dropdown menu) that I can't seem to figure out how to map.

Attempted Solution

As of right now I haven't tried to implement anything. I am unsure where the best place for the IEnumerable<SelectListItem> or where to populate it. Right now it is being populated in the Controller.

User.cs

public class User
{
    [Key]
    public int UserID { get; set; }

    public string Username { get; set; }

    public string Password { get; set; }

    public int RoleID { get; set; }

    [ForeignKey("RoleID")]
    public virtual Role Role { get; set; }
}

EditUserModel.cs

public class EditUserViewModel
{
    [HiddenInput(DisplayValue = false)]
    public int UserID { get; set; }

    [Required]
    public String Username { get; set; }

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [DisplayName("Role")]
    [Required]
    public int RoleID { get; set; }

    //The trouble field
    public IEnumerable<SelectListItem> Roles { get; set; }
}

Controller.cs

EditUserViewModel model = new EditUserViewModel();
//Population of the dropdown menu
model.Roles = context.Roles
    .ToList()
    .Select(x => new SelectListItem
    {
        Text = x.Description,
        Value = x.RoleID.ToString()
    });
//Mapping that the automaper will take care of
model.UserID = user.UserID;
model.Username = user.Username;
model.RoleID = user.RoleID;

解决方案

For the record, here is what I was talking about in the comments to Jakub's answer:

public static class EnumerableExtensions
{
    public static IEnumerable<SelectListItem> ToSelectList<T, TTextProperty, TValueProperty>(this IEnumerable<T> instance, Func<T, TTextProperty> text, Func<T, TValueProperty> value, Func<T, bool> selectedItem = null)
    {
        return instance.Select(t => new SelectListItem
        {
            Text = Convert.ToString(text(t)),
            Value = Convert.ToString(value(t)),
            Selected = selectedItem != null ? selectedItem(t) : false
        });
    }
}

Needless to say, this is dramatically simpler and accomplishes the same thing (and is actually more robust in the event that the property paths are non-simple, since Jakub's solution will not work with nested properties).

(This is not really an answer, I'm posting it as a community wiki just to help elaborate a point)

这篇关于Automapper映射的IEnumerable&LT; SelectListItem&GT;对于下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆