c# - ASP.NET MVC unwanted values when binding dictionary -


this question has answer here:

i need action receives data follows: /contentpages/mypage?id=1&param[test1]=testvalue1&param[test2]=testvalue2

so, wrote action code , works fine:

public actionresult mypage(int id, dictionary<string, string> param) {   foreach (var pair in param)   {     //logger prints string file     logger.trace("{0}: {1}", pair.key, pair.value);   }   return view(); } 

with url above prints:

test1: testvalue1 test2: testvalue2 

but when don't pass param (/contentpages/mypage?id=1) prints:

controller: contentpages action: mypage 

now i'm using code:

public actionresult mypage(int id) {   foreach (var key in request.querystring.allkeys)   {      if (key.startswith("param["))      {          logger.trace("{0}: {1}", key, request.querystring[key]);      }   }   return view(); } 

i want understand i'm doing wrong in first example?

although can't why mvc framework binding action , controller values, can suggest way behavior (i think) want: add [fromquery] attribute parameter want model bind. this:

public actionresult mypage(int id, [fromquery] dictionary<string, string> param) 

this ensures model binding occurs query string, , not anywhere else (e.g. request body, or - suspect happens when don't include query string - route parameters).


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -