Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

asp.net mvc - How to get data for a dropdownlist into viewmodel when using AutoMapper/AutoMapViewResult

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...