How to disable binding of the route values in ASP.NET MVC?

后端 未结 2 1543
悲哀的现实
悲哀的现实 2021-01-14 10:30

As I know ValueProviderDictionary takes values for binding from 3 places

  1. From Post form
  2. From Route values
  3. From Query string

I

相关标签:
2条回答
  • 2021-01-14 11:15

    Override Controller.Initialize() and set the ValueProvider property in that method (after calling base.Initialize()).

    To avoid reimplementing the entire ValueProviderDictionary, you could just subclass it. The only interesting part is that you'd have to copy a small snippet of ValueProviderDictionary.PopulateDictionary(). In your constructor, call the base constructor, then immediately this.Clear(), followed by this.YourCustomPopulateDictionary(). This should make your code much smaller.

    0 讨论(0)
  • 2021-01-14 11:18

    I've made Action attribute and ValueProvider for solving this problem. Named it GetValuesAttribute and StrictValueProviderDictionary resp. You can set the source Form, RouteData or QueryString. For example if you want that ValueProvider would search for values in Form and maybe QueryString write the following above your method or controller:

    [GetValues(ValueSource.Form | ValueSource.QueryString)]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection formValues)
    {
       ...
       UpdateModel(myModel); //model will be updated from mentioned sources
    

    The source code is placed here: http://codepaste.net/2kpzct. The only problem is that it won't work if you'll want to get populated model as a parameter.

    0 讨论(0)
提交回复
热议问题