0

I have an MVC WebApi app. I'm trying to do something basic - pass a string via JSON in the body. My client submits a small number of key/value parameters, and when the MVC router gets them, it begins to interpret the content of the strings.

An example JSON body is

{ "myKey":"red,yellow,brown,orange","foo":"bar" }

My MVC controller method is

public Dictionary<string, string> PostMyAction([FromBody] str1, [FromBody] str2) { }

I would expect str1 == "red,yellow,brown,orange" but instead I get "Can't bind multiple parameters ('str1') to the request's content."

Why is it parsing the first string as a list of parameters?

4

1 回答 1

0

您的 Controller 方法不应该是 ActionResult 吗?您确实可以在您的操作中接收多个参数,但对于清晰的代码,我建议使用一个 ViewModel,其中包含将代表您的视图的属性,并在提交时绑定。

于 2013-03-18T22:30:31.160 回答