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 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:

原文地址:https://www.cnblogs.com/chucklu/p/11650031.html

时间: 2024-08-03 07:59:00

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

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: ''}; l

Posting JSON to Spring MVC Controller

Spring MVC can be setup to automatically bind incoming JSON string into a Java object. Firstly, ensure you have jackson-mapper-asl included on the classpath: <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-

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

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

原文:ASP.NET MVC – 关于Action返回结果类型的事儿(上) 本文转自:博客园-文超的技术博客 一.         ASP.NET MVC 1.0 Result 几何? Action的返回值类型到底有几个?咱们来数数看. ASP.NET MVC 1.0 目前一共提供了以下十几种Action返回结果类型: 1.       ActionResult(base) 2.       ContentResult 3.       EmptyResult 4.       HttpUnau

NHibernate中Session与ASP.NET MVC中Action的综合使用

NHibernate的基本特征是完成面向对象的程序设计语言到关系数据库的映射,在NHibernate中使用持久化对象PO(Persistent Object)完成持久化操作,对PO的操作必须在Session管理下才能同步到数据库, 但是这里的Session并非指HttpSession,可以理解为基于ADO.NET的Connnection,Session是NHibernate运作的中心,对象的生命周期.事务的管理.数据库的存取都与Session息息相关,首先,我们需要知道, SessionFact

MVC中Action的执行过程

接着上一篇:MVC控制器的激活过程 一.代码现行,该伪代码大致解析了Action的执行的过程 try { Run each IAuthorizationFilter's OnAuthorization() method if(none of the IAuthorizationFilters cancelled execution) { Run each IActionFilter's OnActionExecuting() method Run the action method Run ea

MVC中Action参数绑定的过程

一.题外话 上一篇:MVC中Action的执行过程 ControllerContext 封装有了与指定的 RouteBase 和 ControllerBase 实例匹配的 HTTP 请求的信息. 二.Model绑定者 2.1相关说明 http请求中的参数绑定到Model,是由实现了IModelBinder的类来完成的.我们称这样的类叫做Model绑定者 using System; namespace System.Web.Mvc { /// <summary>Defines the metho

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 = [{ Dom

EF5(7) 后台使用SelectListItem传值给前台显示Select下拉框;mvc后台Action接收浏览器传值的4种方式; 后台Action向前台View视图传递数据的四种方式

一:后台使用SelectListItem 传值给前台显示Select下拉框 我们先来看数据库的订单表,里面有3条订单,他们的用户id对应了 UserInfo用户表的数据,现在我们要做的是添加一个Order控制器,显示订单列表,并且在修改订单的时候,把用户的id 用 select 下拉框显示出来,并且可以提交修改数据   1.1 我们通过比较原始的方法,来把数据 传递到前台后,前台使用  循环来显示 select 并且显示是哪个元素被选中 我们在前台的cshtml中,使用 @model 命令 指定