How to get data for a dropdownlist into viewmodel when using AutoMapper/AutoMapViewResult

十年热恋 提交于 2019-11-28 21:39:23

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

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!