Controller's action with dictionary as parameter stripping to dot
I have an Action that receives a class with a dictionary in its properties:
public ActionResult TestAction(TestClass testClass)
{
return View();
}
public class TestClass
{
public Dictionary<string, string> KeyValues { get; set; }
}
If I do a post to my action with the following JSON:
{
"KeyValues": {
"test.withDoT": "testWithDot"
}
}
The key in my dictionary is stripped to the dot and has nothing in the value.
Trying without the dot works. How can I do a post with a dot in a Dictionary<string, string>
with MVC?
We gave a blind try supposing that there is a regex parser somewhere in the deep (well it was minimal the chance) and to escape 'dot'.
After thinking a while I concluded: dot is not a legal char in identifiers. Yes I know it is a key in the C# dictionary, but in the json part (and javascript) it could be in the identifier syntax role.
So I strongly suggest to replace client side the .
(dot) with an escape sequence like _dot_
and replace back it in server side. Performance will suffer of course.
Change the javascript to
var data = { 'KeyValues[0].Key': 'test.withDoT', 'KeyValues[0].Value': 'testWithDot' };
and then post using
$.post('@Url.Action("TestAction")', data, function(data) { ...`
链接地址: http://www.djcxy.com/p/86884.html
上一篇: 如何获得iPhone和配对Apple Watch之间的当前距离
下一篇: 控制器将字典作为参数剥离的动作