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

  • 相关阅读:
    mfc启动画面
    个人冲刺第十天
    个人冲刺第九天
    个人冲刺第八天
    个人冲刺第七天
    个人冲刺第六天
    新一周冲刺计划2
    新一周冲刺
    创意1
    团队绩效与目标
  • 原文地址:https://www.cnblogs.com/chucklu/p/11650031.html
Copyright © 2020-2023  润新知