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 action.

To solve your requirement, here is the working solution -

 解答2

fwiw, this didn't work for me until I had this in the ajax call:

contentType: "application/json; charset=utf-8",

using Asp.Net MVC 4.

解答3

There are a couple issues here. First, you need to make sure to bind your JSON object back to the model in the controller. This is done by changing

data: JSON.stringify(usersRoles),

to

data: { model: JSON.stringify(usersRoles) },

Secondly, you aren't binding types correctly with your jquery call. If you remove

contentType: "application/json; charset=utf-8",

it will inherently bind back to a string.

All together, use the first ActionResult method and the following jquery ajax call:

    jQuery.ajax({
type: "POST",
url: "@Url.Action("AddUser")",
dataType: "json",
data: { model: JSON.stringify(usersRoles) },
success: function (data) { alert(data); },
failure: function (errMsg) {
alert(errMsg);
}
});

Posting JSON Data to ASP.NET MVC

Take a look at Phil Haack's post on model binding JSON data.

The problem is that the default model binder doesn't serialize JSON properly. You need some sort of ValueProvider OR you could write a custom model binder:

How to receive JSON as an MVC 5 action method parameter的更多相关文章

  1. 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 ...

  2. Posting JSON to Spring MVC Controller

    Spring MVC can be setup to automatically bind incoming JSON string into a Java object. Firstly, ensu ...

  3. 白话学习MVC(八)Action的执行二

    一.概述 上篇博文<白话学习MVC(七)Action的执行一>介绍了ASP.NET MVC中Action的执行的简要流程,并且对TempData的运行机制进行了详细的分析,本篇来分析上一篇 ...

  4. jquery ajax/post/get 传参数给 mvc的action

    jquery ajax/post/get 传参数给 mvc的action1.ActionResult Test1    2.View  Test1.aspx3.ajax page4.MetaObjec ...

  5. [转载]jquery ajax/post/get 传参数给 mvc的action

    jquery ajax/post/get 传参数给 mvc的action 1.ActionResult Test1     2.View  Test1.aspx 3.ajax page 4.MetaO ...

  6. ASP.NET MVC – 关于Action返回结果类型的事儿(上)

    原文:ASP.NET MVC – 关于Action返回结果类型的事儿(上) 本文转自:博客园-文超的技术博客 一.         ASP.NET MVC 1.0 Result 几何? Action的 ...

  7. MVC中Action参数绑定的过程

    一.题外话 上一篇:MVC中Action的执行过程 ControllerContext 封装有了与指定的 RouteBase 和 ControllerBase 实例匹配的 HTTP 请求的信息. 二. ...

  8. MVC中Action的执行过程

    接着上一篇:MVC控制器的激活过程 一.代码现行,该伪代码大致解析了Action的执行的过程 try { Run each IAuthorizationFilter's OnAuthorization ...

  9. 白话学习MVC(七)Action的执行一

    一.概述 在此系列开篇的时候介绍了MVC的生命周期 , 对于请求的处理,都是将相应的类的方法注册到HttpApplication事件中,通过事件的依次执行从而完成对请求的处理.对于MVC来说,请求是先 ...

随机推荐

  1. iOS-H5交互综合整理

    1.WKWebView的使用 2.常见问题 2.1 iOS开发 WKWebView下js的alert(),confirm(),prompt()方法无法正常执行

  2. 如何通过Restful API的方式读取SAP Commerce Cloud的Product Reference

    从SAP官网上找到api的说明: https://api.sap.com/api/commerce_services/resource api endpoint: /rest/v2/electroni ...

  3. 关于近期使用webpack所引发的思考

    近期,(使其也挺长时间了).使用了一段时间的webpack. 但是在使用期间个人感觉并不想网上说的那样好,个人对webpack的评价并不是很高,甚至有点反感使用webpack. 个人感觉使用webpa ...

  4. springboot2.1.3使用jdbcTemplate

    这里只是备忘一下使用方式,至于配置数据源信息不在此文中讲解,忘谅解. 1.  查询返回List<Long>数据集 (这里比如返回userId,long型) @Autowired@Quali ...

  5. 【转】CCS5.5从硬盘读入.dat数据格式的单张图像

    首页 博客 学院 CSDN学院                            下载 论坛 APP CSDN                            问答 商城 活动 VIP会员 ...

  6. atoi()和itoa()函数详解以及C语言实现

    atoi()函数 atoi()原型:  int atoi(const char *str ); 函数功能:把字符串转换成整型数. 参数str:要进行转换的字符串 返回值:每个函数返回 int 值,此值 ...

  7. spark HMM

    Scala实现的: https://github.com/skrusche63/spark-intent/tree/master/src/main/scala/de/kp/scala/hmm http ...

  8. (三)WCF之契约

    自从我们接收ITOO项目之后,就必须跟WCF打交道,既然必须直面WCF,我就对WCF进行了研究(还不是很深入,是自己目前的理解).之前对WCF的一些基础知识进行了简单的介绍,本篇重点来介绍WCF的契约 ...

  9. keras模块学习之泛型模型学习笔记

    本笔记由博客园-圆柱模板 博主整理笔记发布,转载需注明,谢谢合作! Keras泛型模型接口是:  用户定义多输出模型.非循环有向模型或具有共享层的模型等复杂模型的途径  适用于实现:全连接网络和多输入 ...

  10. test20190803 夏令营NOIP训练19

    60+100+0=160 贪婪大陆 面对蚂蚁们的疯狂进攻,小FF的Tower defence宣告失败--人类被蚂蚁们逼到了Greed Island上的一个海湾.现在,小FF的后方是一望无际的大海, 前 ...