简体   繁体   中英

Mapping Model Properties into Controller

I've searched the whole day for a solution of my problem, but didn't find anything acceptable...

My project is built using the MVC Pattern and I'm not sure if I can make the model directly accessible for the view. As far as I know, it's intended to "hide" the model from the view and use the controller as a "proxy".

I show you an example for better understanding of my problem.

The model:

internal class ExampleModel 
{
    public Size GridSize {get; set;}
    //a lot more properties
    //...
    //business logic
}

The controller:

internal class ExampleController 
{
    private ExampleModel m_Model;

    //Logic "proxy" with some extensions
    //A lot of functions
    //--------------------
    //Mapping the model's properties
    public Size GridSize 
    {
        get
        {
            return m_Model.GridSize;
        }
        set
        {
            m_Model.GridSize = value;
        }
    }
    //A lot more properties, mapping the ones from the model
}

So my question is: How I can auto-map the model's properties to the controller? Because "synchronizing" the properties between the model and controller ... sucks? :P Or could I just make the m_Model public? I really don't know what to do..

Thanks in advance :)

Typically, you would pass your model into the view at the point that your return it in your controller action.

public class HomeController
{
   public ActionResult Index()
   {
      // Do something with model here, in this example we are creating a new model
      var model = new Model(); 

      // Send the model to the view, this is then available as @Model
      return View(model);
   }
}

Note that there might be additional properties that your view requires that you don't wish to polute your model with. In this instance, you can create a view model which augments your model with the additional properties. Your view model would typically contain a reference to your model.

Some purists don't like to expose the model to the view, so this link provides some additional approaches:

http://blogs.msdn.com/b/simonince/archive/2010/01/26/view-models-in-asp-net-mvc.aspx

devdigital's answer is correct, you shouldn't really be exposing your Model thru the Controller (in design terms, it forces your View to know something about the controller which beats the purpose...). Your Controller should prepare all the data your View will need and pass it along when creating the ViewResult (which is what the return View(model) does for the default View name).

However, you also asked if there's a better way to map properties between objects. I don't think devdigital's link mentions this particular tool (but it does mention mappers): AutoMapper ( http://automapper.codeplex.com/ ).

You first configure a Map between 2 entities/models/objects of any kind. The default behavior will simply assign properties with the same name but you can then configure to ignore properties, or customize the value assignment.

AutoMapper.Mapper.CreateMap<ExampleEntity, ExampleModel>();

Then you just use it...

ExampleModel model = AutoMapper.Mapper.Map<ExampleModel>(myEntity);

This will work for 1:1 entity mapping though. I wouldn't recommend using AutoMapper if you are actually augmenting a ViewModel (ViewModel is what you would use to provide a view with data that comes from different entities or sources in general).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM