开发者

Posting complex data to MVC controllers/actions

I've already done research on how to post data to MVC controllers/actions and I'm running into a bit of trouble. I wish to post data (via javascript), to my MVC Controller, in this format:

{
  someString: "thisString",
  myArray: ["string1", "string2"]
}

My MVC Action 开发者_JAVA技巧has the following signature:

[HttpPost]
public ActionResult someAction(FormCollection formValues);

If I check the values of formValues, I'll see key/value pairs:

key: "someString"
value: "thisString"

key: "myArray[]"
value: [0]: "string1", [1]: "string2"

It all looks good untill I try:

TryUpdateModel(MyCustomModel);

The key/value pair someString/"thisString" binds perfects but the array (myArray) resolves to null.

My Model looks like this:

public MyCustomModel 
{
    public string someString {get; set;}
    public string [] myArray {get; set;}
}

Here is how my post looks like (jQuery):

$.post
(
    "Controller/someAction",
    {
        someString: "thisString",
        myArray: ["string1", "string2"],
    },
    function(data) { ...do something }
); 

What on earth am I doing wrong? Does the default MVC ModelBinder not support binding a simple array of strings?


You must add the following when posting to your controller action

traditional: true

I just replicated the issue on my machine and that solved it. I also found this post which seems to have contained the same solution

Edit

Here's another similar question...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜