Controller action with a dictionary as a parameter, stripping to a point

I have an action that gets 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 make a message for my action with the following JSON:

 { "KeyValues": { "test.withDoT": "testWithDot" } } 

The key in my dictionary is split into a dot and nothing matters.

enter image description here

Trying without using dots. How can I make a message with a dot in Dictionary<string, string> with MVC?

+7
c # asp.net-mvc
source share
2 answers

We gave a blind attempt to suggest that there is a regular expression parser somewhere in the back (well, that was the minimum chance) and avoid the “dot”.

After thinking for a while, I came to the conclusion: the point is not a legal char in identifiers. Yes, I know that this is the key in the C # dictionary, but in the json part (and javascript) it can be in the role of identifier syntax.

Therefore, I highly recommend replacing the client side . (dot) to an escape sequence like _dot_ and replace it on the server side. Of course, performance will suffer.

+3
source share

Change javascript to

 var data = { 'KeyValues[0].Key': 'test.withDoT', 'KeyValues[0].Value': 'testWithDot' }; 

and then send a message using

 $.post('@Url.Action("TestAction")', data, function(data) { ...` 
+1
source share

All Articles