如何使用时,获得的数据将DropDownList到视图模型AutoMapper / AutoMapViewResult [英] How to get data for a dropdownlist into viewmodel when using AutoMapper/AutoMapViewResult
问题描述
在行动A>阅读 ASP.NET MVC 2和看的从mvcConf (均强烈推荐!),我开始实施他们的一些想法。吉米·博加德的presentation
After reading ASP.NET MVC 2 in Action and watching Jimmy Bogard's presentation from MvcConf (both highly recommended!), I began to implement some of their ideas.
一个他们做的很酷的事情,不仅要使用AutoMapper你的实体映射到某个视图模型,但有一个AutoMapViewResult自动完成:
One of the cool things they do, is not only to use AutoMapper to map your entities to some viewmodel, but automate this with an AutoMapViewResult:
public class EventsController : BaseController
{
public ActionResult Show(Event id) // EntityModelBinder gets Event from repository
{
return AutoMapView<EventsShowModel>(id); // AutoMapView<T>(model) is a helper method on the BaseController, that calls AutoMapViewResult<T>(...)
}
}
// not exactly what you'll find in the book, but it also works :-)
public class AutoMapViewResult<TDestination> : ViewResult
{
public AutoMapViewResult(string viewName, string masterName, object model)
{
ViewName = viewName;
MasterName = masterName;
ViewData.Model = Mapper.Map(model, model.GetType(), typeof(TDestination));
}
}
这一切的伟大工程,但现在有一个修改
操作的 EventsEditModel
:
This all works great, but now there's a Edit
action with its EventsEditModel
:
public class EventsEditModel
{
// ... some properties ...
public int LocationId { get; set; }
public IList<SelectListItem> Locations { get; set; }
}
public class EventsController : BaseController
{
public ActionResult Edit(Event id)
{
return AutoMapView<EventsEditModel>(id);
}
}
现在(终于)问题:
你觉得什么,是让某些类型的数据源,如存储库的位置的最佳途径的
属性? EventsEditModel
位置
What do you think, is the best way to get the locations from some sort of data source such as a repository to the EventsEditModel
's Locations
property?
请记住,我想使用 AutoMapViewResult
和很多不同的实体视图模型组合。
Keep in mind, that I want to use the AutoMapViewResult
and a lot of different entity-viewmodel combinations.
更新:
我去丧尸的想法,并创建了一个自定义属性。你可以看一下在code和下载它在我的博客<一href=\"http://blog.opo.li/post/ASPNET-MVC-Loading-data-for-select-lists-into-edit-model-using-attributes.aspx\">ASP.NET MVC:加载数据选择列表进入编辑模式使用属性
I went with Necros' idea and created a custom attribute. You can look at the code and download it on my blog ASP.NET MVC: Loading data for select lists into edit model using attributes.
推荐答案
我没有得到的点(因为我看到了谈话),当我需要这一点,但我心里有这个可能的解决方案。我认为这将努力创造一个属性,指定这个属性需要加载。我会先从一个抽象类:
I haven't gotten to the point (since I saw the talk) when I needed this, but I have a possible solution for this in mind. I think it would work to create an attribute, specifying that this property needs to be loaded. I would start with an abstract class:
public abstract class LoadDataAttribute : Attribute
{
public Type Type { get; set; }
protected LoadDataAttribute(Type type)
{
Type = type;
}
public abstract object LoadData();
}
那么对于要加载每个类型创建特定的版本(在你的案件位置)
Then create specific version for each type you want to load (Locations in your case)
public class LoadLocationsAttribute : LoadDataAttribute
{
public LoadLocationsAttribute() : base(typeof(IList<SelectListItem>))
public override object LoadData()
{
// get locations and return IList<SelectListItem>
}
}
在你的的ExecuteReuslt
AutoMappViewResult
你会找到 LoadDataAttribute $的所有属性C $ C>,电话
LoadData()
,将它转换为类型属性中指定,并将其分配给属性。
In your ExecuteResult
of AutoMappViewResult
you would find all properties with LoadDataAttribute
, call LoadData()
, cast it to type specified in the attribute and assign it to the property.
我如果你只是要加载选择列表这种方式,你可以只返回的IList&LT; SelectListItem&GT;
而不是对象$ C $的C>,并保存自己一些麻烦铸造。
I case you just want to load select lists this way, you can just return IList<SelectListItem>
instead of object
, and save yourself some trouble with casting.
您的视图模型将在明显的使用属性。
Your view model would the obviously use the attribute.
public class EventsEditModel
{
// ... some properties ...
public int LocationId { get; set; }
[LoadLocations]
public IList<SelectListItem> Locations { get; set; }
}
这篇关于如何使用时,获得的数据将DropDownList到视图模型AutoMapper / AutoMapViewResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!