directly receive json data from javascript in mvc

if you send json data to mvc,how can you receive them and parse them more simply?

you can do it like this:

            latestData = [];
            $(‘.save‘).click(function () {
                $(‘.content tr‘).each(function () {
                    var item = { id:null,date: ‘‘, weekday: ‘‘, holiday: ‘‘};                     latestData.push(item);
                });
                $.post(url , JSON.stringify(latestData) , function (data) {});
             });

if you have a entity which has same properties with the ‘item‘,then you can parse json like this:

 IList<Holidays> holidays = new JavaScriptSerializer().Deserialize<Holidays[]>(ControllerContext.HttpContext.Request.Form[0]);
            

pay attention to red line,don‘t need any pramaters in action,deserialized type is an array.

时间: 2024-08-28 07:47:37

directly receive json data from javascript in mvc的相关文章

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

Google Volley: How to send a POST request with Json data?

sonObjectRequest actuallyaccepts JSONObject as body. From http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/ final String url = "some/url"; final JSONObject jsonBody = /* ... */; new JsonObjectRequest(url, jsonBody,

jQuery解析JSON出现SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data 我在使用$.parseJSON解析后台返回的JSON的数据时,出现了这样的错误,我还以为返回的JSON格式出现了错误,因为JSON要求格式非常严格.最后发现JSON格式没有太明显的格式错误,我使用fastJSON来生成的JSON格式数据,原来是因为数据已经是一个JavaScript对象了,所以在进行解析就会出错了 我直接将这段数据al

How to get blob data using javascript XmlHttpRequest by sync

Tested: Firefox 33+ OK Chrome 38+ OK IE 6 -- IE 10 Failed Thanks to 阮一峰's blog: http://www.ruanyifeng.com/blog/2012/09/xmlhttprequest_level_2.html The sample shows how to get blob data using javascript XmlHttpRequest by sync. The w3c tell us cannot s

PHP convet class to json data

/********************************************************************* * PHP convet class to json data * 说明: * 突然想使用class自动转换为json数据,这样的代码可扩展性会好一点, * 只需要修改class的属性就能够达到最终json数据输出,不过有遇到class中 * 初始化class变量需要在构造函数中初始化的的问题. * * 2017-8-11 深圳 龙华樟坑村 曾剑锋 ***

JSON不是JavaScript对象

一篇短小精悍的文章,我觉得不错就翻译了. 原文作者 Fizer Khan 原文地址 http://www.fizerkhan.com/blog/posts/JSON-is-not-Javascript-Object.html 很多人都把JSON当作JavaScript对象,但它根本不是.JSON只是一种灵感来自JavaScript对象结构的字符串表示法.JSON被设计得简单松散,用来在服务器和浏览器之间传输数据.因为它的简洁性,也在浏览器和服务器之外的其他应用中被使用. 我尝试拷贝一些JavaS

键值对参数转换为Jquery json data参数

键值对:var postData = new List<KeyValuePair<string, string>>();postData.Add(new KeyValuePair<string, string>("a", "1"));postData.Add(new KeyValuePair<string, string>("b", "2"));postData.Add(new

javascript simple MVC

<h3>javascript simple MVC</h3> <div> <select name="" id="setAnimal"> <option value="cat">cat</option> <option value="fish">fish</option> <option value="bird&qu

json data 解析demo

json data: demo: JsonObject jsonObject= JsonHandle.getAsJsonObject(city_dataInfo).get("data").getAsJsonObject().get("cityList").getAsJsonObject().get("items").getAsJsonObject(); Set<Entry<String, JsonElement>> ent