How to bind URL parameters to model properties with different names

后端 未结 5 1181
你的背包
你的背包 2020-12-16 01:18

Okay, lets say I have a URL like so, which is mapped via HTTP verb GET to the controller action I have below:

GET /foo/bar?sort=asc&r=true
<         


        
5条回答
  •  旧时难觅i
    2020-12-16 01:41

    There is no free MVC.Net Modelbinding feature that gives you what you want out of the box. When I need to do something like this, it really makes me think about my modeling which almost always makes me create a ViewModel for my view binding and a EntityModel for my repository storage.

    I like to use AutoMapper to convert between these different types. Best part of using AutoMapper is that it drives you away from having to write the mapping logic yourself over and over for each Action in your controller. Just set it up once with AutoMapper in your initialization sections and just execute something like this in the future.

    Mapper.Map(barViewModel);
    

提交回复
热议问题