Posting array of JSON objects to MVC3 action method via jQuery ajax

Does the model binder not suport arrays of JSON objects? The code below works when sending a single JSON domain object as part of the ajax post. However, when sending an array of JSON domain objects, the action parameter is null.

     var domains = [{
                        DomainName: ‘testt1‘,
                        Price: ‘19.99‘,
                        Available: true
                    }, {
                        DomainName: ‘testt2‘,
                        Price: ‘15.99‘,
                        Available: false
                    }];

                $.ajax({
                    type: ‘POST‘,
                    url: Url.BasketAddDomain,
                    dataType: "json",
                    data: domains,
                    success: function (basketHtml) {

                    },
                    error: function (a, b, c) {
                        alert(‘A problem ocurred‘);
                    }
            });

This is the action method:

public ActionResult AddDomain(IEnumerable<DomainBasketItemModel> domain)
{
    ...

Any ideas if it is possible to do this?

Answers

You need:

var domains = { domains: [... your elements ...]};

            $.ajax({
                type: ‘post‘,
                url: ‘Your-URI‘,
                data: JSON.stringify(domains),
                contentType: "application/json; charset=utf-8",
                traditional: true,
                success: function (data) {
                    ...
                }
            });

In general, check out the Request object in the debugger, you‘ll see what‘s being passed and get an idea of how to "speak" HTTP.

NOTE: Just found this out. The model binder chokes on nullable decimal properties like:

public decimal? latitude { get; set; }

So it won‘t bind that if you‘re posting to it with a json string that looks like this:

{"latitude":12.0}

But it WILL work if you post something like this:

{"latitude":"12.0"}

See: http://syper-blogger.blogspot.com/2011/07/hello-world.html

source:http://stackoverflow.com/questions/6031206/posting-array-of-json-objects-to-mvc3-action-method-via-jquery-ajax

时间: 2024-08-03 21:06:12

Posting array of JSON objects to MVC3 action method via jQuery ajax的相关文章

How to receive JSON as an MVC 5 action method parameter

How to receive JSON as an MVC 5 action method parameter 解答1 Unfortunately, Dictionary has problems with Model Binding in MVC. Read the full story here. Instead, create a custom model binder to get the Dictionary as a parameter for the controller acti

Sending JSON to an ASP.NET MVC Action Method Argument

Sending JSON to an ASP.NET MVC Action Method Argument 原文地址:https://www.cnblogs.com/chucklu/p/11650080.html

python np array转json

np array转json import numpy as np import codecs, json a = np.arange(10).reshape(2,5) # a 2 by 5 array b = a.tolist() # nested lists with same data, indices file_path = "/path.json" ## your path variable json.dump(b, codecs.open(file_path, 'w', en

再谈Jquery Ajax方法传递到action 【转载】

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://cnn237111.blog.51cto.com/2359144/984466 之前写过一篇文章Jquery Ajax方法传值到action,本文是对该文的补充. 假设 controller中的方法是如下: public ActionResult ReadPerson(PersonModel model)  {  string s = model.ToString();  ret

再谈Jquery Ajax方法传递到action(转)

之前写过一篇文章Jquery Ajax方法传值到action,本文是对该文的补充. 假设 controller中的方法是如下: public ActionResult ReadPerson(PersonModel model) { string s = model.ToString(); return Content(s); } public ActionResult ReadPersons(List<PersonModel> model) { string result = "&q

分享在MVC3.0中使用jQuery DataTable 插件

前不久在网络上看见一个很不错的jQuery的DataTable表格插件.后来发现在MVC中使用该插件的文章并不多.本文将介绍在MVC3.0如何使用该插件.在介绍该插件之前先简单介绍一下,推荐该插件的原因.在项目中我使用jqgrid比较多.但是发现当进行样式调整时jqgrid的样式常常会让美工头疼.而datatable插件却是一个轻量级的jQuery插件.当我通过浏览器查看该js插件rander后的源码.发现只是一个简单的html table,非常简洁.那么在没有特殊要求的情况下使用这个插件,开发

基于MVC3下拉列表联动(JQuery)

MVC3 上次项目中遇到一个需要多个下拉列表联动的操作,今天有空将实现方式整理以便以后参考. 要达到的效果是,点击一个下拉框,则另一个下拉框的值发生对应变化.如:选择中国,则另个一下拉框里显示中国各个省份. 传统的HTML方式比较简单,实际上基于MVC的实现方式也大同小异. 直接上代码: public class DP_Provice     {        public int proviceID { get; set; }        public string ProviceName

MVC中使用Ajax提交数据 Jquery Ajax方法传值到action

Jquery Ajax方法传值到action <script type="text/javascript"> $(document).ready(function(){ $("#btn").click(function(){ $.ajax({ type: 'POST', url: "/Home/MyAjax", data: { val1: $("#txt1").val(), val2: $("#txt2&

php+jquery+ajax+json的一个最简单实例

function ajaxTest(){    $username = $_POST['username'];$age = $_POST['age'];$job = $_POST['job'];$json_arr = array("username"=>$username,"age"=>$age,"job"=>$job);$json_obj = json_encode($json_arr);echo $json_obj;