使用 AutoMapper/AutoMapViewResult 时如何将下拉列表的数据获取到视图模型中

发布于 2024-09-14 22:58:18 字数 1878 浏览 5 评论 0原文

阅读ASP.NET MVC 2 实践并观看Jimmy Bogard 在 MvcConf 的演讲(强烈推荐!),我开始实现他们的一些想法。

他们所做的一件很酷的事情,不仅是使用 AutoMapper 将您的实体映射到某个视图模型,而且还使用 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));
    }
}

这一切都很好,但现在有一个 Edit 操作及其 < code>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); 
    }
}

现在(最后)问题:

您认为从某种数据源(例如存储库)获取位置到 EventsEditModelLocations 属性?

请记住,我想使用 AutoMapViewResult 和许多不同的实体视图模型组合。

更新:

我采纳了 Necros 的想法并创建了一个自定义属性。你可以在我的博客上查看代码并下载 ASP.NET MVC:使用属性将选择列表的数据加载到编辑模型中

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.

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));
    }
}

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); 
    }
}

And now (finally) the question:

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?

Keep in mind, that I want to use the AutoMapViewResult and a lot of different entity-viewmodel combinations.

Update:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

深陷 2024-09-21 22:58:19

我建议您从此处asp.net-mvc示例应用程序 > 这比 automaper 简单得多

特别是看看 TinyController

I would recommend you to look at the asp.net-mvc sample application from here which does this much much simpler than automaper

Especially look at the TinyController

黯然#的苍凉 2024-09-21 22:58:19

好吧,将带有参数和属性的构造函数添加到控制器中,并使用 DI(我个人喜欢 Ninject)注入正确的存储库实现:

public IEventsRepository _repo;

public EventsController(IEventsRepository repository)
{
  _repo = repository;
}

在 Ninject 应用程序和站点模块中的 global.asax.cs 中连接(绑定)依赖项(如果您需要包含该答案的扩展答案,请告诉我),

然后在您的编辑操作中使用存储库来获取位置。假设您的存储库接口上有 LoadLocations() 方法,并且在例如 SqlEventsRepository(实现 IEventsRepository)中具有该方法,您只需调用该方法即可完成此操作:

public ActionResult Edit(Event id)
{
...
EventsEditModel model = new EventsEditModel();
_repo.GetInstance(id);
model.Locations = _repo.LoadLocations();
...
}

我正在弥补这一点,因为您没有提供太多信息。当您想在将实体映射到 ViewModel 之前从数据存储加载一些附加数据时,我不知道 Automapper 的具体情况。

另外,您没有指定此编辑操作是 GET 还是 POST,但我假设它是 GET。假设它确实是 GET,我不知道如何通过向操作提供实体来加载任何内容。

最常见的是,GET 方法使用 string 或 int 类型的参数(很可能是某种 slugs 或 id),POST 方法使用 ViewModel 类型(而不是 Entity)参数。

所以你的 POST 方法签名应该像这样

[HttpPost]
public ActionResult Edit(EventsEditModel model)...

我曾经在我的操作签名中直接使用实体并且一直失败,所以我现在不鼓励其他人这样做。

华泰

Well, add a constructor with a parameter and a property into your controller and use DI (personally I like Ninject) to inject the correct repository implementation:

public IEventsRepository _repo;

public EventsController(IEventsRepository repository)
{
  _repo = repository;
}

Wire (bind) the dependencies up in the global.asax.cs in Ninject application and site module (if you need expanded answer with that included, please let me know),

then in your Edit action use the repository to get the Locations. Suppose you have the LoadLocations() method on your repository interface and concrete implementation of it in, for instance, SqlEventsRepository (implements IEventsRepository), you do it simply by calling the method:

public ActionResult Edit(Event id)
{
...
EventsEditModel model = new EventsEditModel();
_repo.GetInstance(id);
model.Locations = _repo.LoadLocations();
...
}

I am making this up because you have not provided too much information. And I don't know Automapper specifics when you want to load some additional data from the datastore prior to mapping the Entity to the ViewModel.

Also you don't specify whether this Edit action is GET or POST, but I assume it is GET. Assuming that it's really GET, I don't know how can you load anything by providing the Entity to the action.

Most commonly the GET methods use parameters of type string or int (most likely those are slugs or ids of somekind) and POST methos use parameters of type ViewModel (not Entity).

So you POST method signature should be like this

[HttpPost]
public ActionResult Edit(EventsEditModel model)...

I used to use Entities directly in my action signatures and was failing all the time, so I discourage it to others now.

HTH

污味仙女 2024-09-21 22:58:18

我的解决方案是引入模型丰富器的概念,即在模型传递到 View() 之前“丰富”模型的简单类:

public class SiteSettingsModelEnricher : IModelEnricher<SiteSettingsModel>
{
    private readonly IThemeProvider themeProvider;
    public SiteSettingsModelEnricher(IThemeProvider themeProvider) {
        this.themeProvider = themeProvider;
    }

    public SiteSettingsModel Enrich(SiteSettingsModel model) {
        var themes = from t in themeProvider.GetThemes()
                     select new SelectListItem { Text = t, Value = t };
        model.Themes = themes;

        return model;
    }
}

的 AutoMapperViewResult ExecuteResult 方法如下所示:

    public override void ExecuteResult(ControllerContext context) {

        var model = Mapper.Map(this.Model, typeof(TSource), typeof(TDestination)) as TDestination;

        // enrichers
        var enricher = DependencyResolver.Current.GetService<IModelEnricher<TDestination>>();
        if (enricher != null) {
            model = enricher.Enrich(model);
        }

        this.ViewData.Model = model;
        base.ExecuteResult(context);
    }

我 还使用吉米演示中的 FormActionResult 在返回失败结果之前我还使用了丰富器。这意味着像选择列表这样的东西会被重新绑定并保持超级干燥。

[更新]

我在此处发布了一个改进的解决方案就上述而言。

My solution to this was to introduce the concept of Model enrichers, simple classes that "enrich" the model before it is passed to the View():

public class SiteSettingsModelEnricher : IModelEnricher<SiteSettingsModel>
{
    private readonly IThemeProvider themeProvider;
    public SiteSettingsModelEnricher(IThemeProvider themeProvider) {
        this.themeProvider = themeProvider;
    }

    public SiteSettingsModel Enrich(SiteSettingsModel model) {
        var themes = from t in themeProvider.GetThemes()
                     select new SelectListItem { Text = t, Value = t };
        model.Themes = themes;

        return model;
    }
}

My AutoMapperViewResult ExecuteResult method then looks like:

    public override void ExecuteResult(ControllerContext context) {

        var model = Mapper.Map(this.Model, typeof(TSource), typeof(TDestination)) as TDestination;

        // enrichers
        var enricher = DependencyResolver.Current.GetService<IModelEnricher<TDestination>>();
        if (enricher != null) {
            model = enricher.Enrich(model);
        }

        this.ViewData.Model = model;
        base.ExecuteResult(context);
    }

As I'm also using the FormActionResult from Jimmy's presentation I also use the enricher before returning the Failure result. This means that things like select lists are rebinded and keeps things super DRY.

[Update]

I posted an improved solution here that builds on the above.

白云悠悠 2024-09-21 22:58:18

当我需要这个时,我还没有说到重点(自从我看到演讲以来),但我心中有一个可能的解决方案。我认为创建一个属性,指定需要加载该属性是可行的。我将从一个抽象类开始:

public abstract class LoadDataAttribute : Attribute
{
    public Type Type { get; set; }

    protected LoadDataAttribute(Type type)
    {
        Type = type;
    }

    public abstract object LoadData();
}

然后为您要加载的每种类型创建特定版本(您的情况下的位置)

public class LoadLocationsAttribute : LoadDataAttribute
{
    public LoadLocationsAttribute() : base(typeof(IList<SelectListItem>))

    public override object LoadData()
    {
        // get locations and return IList<SelectListItem>
    }
}

AutoMappViewResultExecuteResult 中,您将找到所有属性LoadDataAttribute,调用LoadData(),将其转换为属性中指定的类型并将其分配给属性。

如果您只想以这种方式加载选择列表,您可以只返回 IList 而不是 object,这样就可以省去一些转换的麻烦。

您的视图模型显然会使用该属性。

public class EventsEditModel
{
    // ... some properties ...
    public int LocationId { get; set; }

    [LoadLocations]
    public IList<SelectListItem> Locations { get; set; }
}

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>
    }
}

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.

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