-2

我有一个名为 word 的模型。

这是我的文字模型

public class Word : BaseFieldsTables
{
    int ID { get; set; }
    string Name { get; set; }
    Guid UniqID { get; set; }
    DateTime CreatedDate { get; set; }
    byte[] RowVersion { set; get; }
    public string Text { get; set; }
    public virtual Category Category { get; set; }
    public int CategoryID { get; set; }
    public virtual Language Language { get; set; }
    public int LanguageID { get; set; }

    public virtual ICollection<Picture> Pictures { get; set; }

    [InverseProperty("MainWord")]
    public virtual ICollection<RelationshipBetweenWords> MainWords { get; set; }

    [InverseProperty("RelatedWord")]
    public virtual ICollection<RelationshipBetweenWords> RelatedWords { get; set; }
}

单词有一个类别和语言,并且......

例如这是我的语言模型

    public class Language : BaseFieldsTables
    {
       int ID { get; set; }
        string Name { get; set; }
        Guid UniqID { get; set; }
        DateTime CreatedDate { get; set; }
        byte[] RowVersion { set; get; }
        public virtual ICollection<Word> Words { get; set; }
    }

我有这样的单词视图模型

public class WordViewModel

    public string Name { get; set; }


    public IList<SelectListItem> Categories { set; get; }


    [HiddenInput(DisplayValue = false)]
    public int SelectedCategoryID { set; get; }


    public IList<SelectListItem> Languages { set; get; }



    [HiddenInput(DisplayValue = false)]
    public int SelectedLanguageID { set; get; }
}

这是我的控制器

public ActionResult Index(
    {
        IList<Word> list = _wordService.GetAll();

    }

我想用语言名和类别名称将所有单词和映射(自动映射器)到我的 WordViewModel 并返回到我的视图中,如果我必须更改我的 wordviewmodel 或...以及如何映射.tnx,请帮助我

4

1 回答 1

0

哦,我找到了

public ActionResult Index()
        {
            var orders = _respository.GetAll();

            AutoMapper.Mapper.CreateMap<Order, OrderDto>()
                .ForMember(dest => dest.OrderNumber, opt => opt.MapFrom(src => src.OrderNo))
                .ForMember(dest => dest.OrderItemsDto, opt => opt.Ignore());

            var model = AutoMapper.Mapper.Map<IEnumerable<Order>, IEnumerable<OrderDto>>(orders);

            return View(model);
        }
//Razor View Code
<h2>Orders</h2>
<table>
    <tr>
        <th>Order Number</th>
        <th>Name</th>
        <th>Total</th>
    </tr>
@foreach (var item in Model)
{
    <tr>
        <td>@item.OrderNumber</td>
        <td>@item.CustomerName</td>
        <td>$@item.Total</td>
    </tr>
}
</table>
于 2012-06-28T15:49:44.550 回答