开发者

What parameter does my .NET MVC 2 controller need for a JSON Post

I can't figure out what parameters or type to use in my controller when accepting a POST from an .ajax jquery call which is using Json.

The JSON looks something like this (I just wrote this so it might not be valid, that's not the problem)

[{"paths":[[{"a":294,"b":174,"c":1297178158028}]]

My controller:

[HttpPost]
public void SaveDrawData() {
}

$.ajax('../Home/SaveDrawData', { type: 'POST', contentType: 'application/json', dataType: 'json', processData: false, cache: false, data: serial, success: function() { var saved = $('<div id="sav开发者_StackOverflowed">Saved!</div>')
                    .appendTo('body')
        }

I know for sure that the SaveDrawData is being hit by setting a breakpoint.

I checked firebug, and the JSON post lists "paths".

How does my controller access that data though? What type and/or variable name do I need to put there?

Thanks so much!


Have you tried:

public ActionResult SaveDrawData(string paths)
{
    //do stuff here
}

Also try to access ViewData["paths"] in your controller action. If you add attach the debugger and Add Watch for ViewData, you should be able to see what the view is sending back and what it is calling it.


What about this solution

html code:

<% using (Ajax.BeginForm(new AjaxOptions(){UpdateTargetId = "location", HttpMethod = "POST"})){ //do your stuff here} %>

controller

[AcceptVerbs(HttpVerbs.Post)]
        public JsonResult YourAction()
        {
            IList<KeyValuePair<string, object>> results = new

List>();

        KeyValuePair<string, object> pair1 = new
> KeyValuePair<string, object>("key one",
>                                                                       
> new {testing = "object one testing "});
>                 results.Add(pair1);
>                 //return Json(results, JsonRequestBehavior.AllowGet); 
> //if you want allow get
>                 return Json(results);
>             }

where when submit happenes: there will be request as when you sending html form and you will get results accesible as Request["parameterName"]

or bind it to the model where you will put the model to action of the controller:

EG:

public ActionResult YourAction(FooModel model){ //do your thing }

To read about this, visit this tutorial.. http://lozanotek.com/blog/archive/2010/04/16/posting_json_data_to_mvc_controllers.aspx

I hope this will help.


You can pass an array as a POST parameter, and MVC will detect it as a List of strings.

[HttpPost]
public ActionResult SaveDrawData(List<string> paths)
{
    var someObject = CreateObjectFromPaths(paths);
    return Json(someObject);
}

Also, on your $.ajax jQuery call, try setting traditional: true

UPDATE

I looked more closely at what you're passing into the .ajax call. Looks like your parameter would actually be a List<int> :)


Since you are using MVC 2, you need to reference ASP.NET MVC Futures to receive JSON values. Make sure first that your controller has ASP.NET MVC Futures, then try to JSON-post simple(e.g. integer) types to your controller method as POC that your controller can already receive JSON data; if you've verified your controller can already receive JSON data, then try this structure:

public class Mate
{
   public int a { get; set; }
   public int b { get; set; }
   public int c { get; set; }
};


public void SaveDrawData(Mate[] paths) {
}

JSON-library for ASP.NET MVC 2: ASP.NET MVC Futures: http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜