如何接收JSON在MVC 5操作方法为放慢参数? [英] How to receive JSON in a mvc 5 action method as a paramter?

查看:563
本文介绍了如何接收JSON在MVC 5操作方法为放慢参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在整个下午通过网页抓取尝试接收一个JSON对象的动作控制器尝试。

I have been trying the whole afternoon crawling through the web trying to receive a JSON object in the action controller.

什么是去这样做的正确,或者更简单的方法?

What is the correct and or easier way to go about doing it?

我曾尝试以下操作:
1:

I have tried the following: 1:

    //Post/ Roles/AddUser
    [HttpPost]
    public ActionResult AddUser(String model)
    {
        if(model != null)
        {
            return Json("Success");
        }else
        {
            return Json("An Error Has occoured");
        }

    }

这给了我在我输入一个空值。

Which gave me a null value on my input.

2

    //Post/ Roles/AddUser
    [HttpPost]
    public ActionResult AddUser(IDictionary<string, object> model)
    {
        if(model != null)
        {
            return Json("Success");
        }else
        {
            return Json("An Error Has occoured");
        }

    }

这使我对jQuery的一侧是试图张贴到它500错误? (这意味着它未正确绑定)。

which gives me a 500 error on the jquery side which is trying to post to it? (meaning that it didn't bind correctly).

这是我的jQuery code:

here is my jQuery code:

<script>
function submitForm() {

    var usersRoles = new Array;
    jQuery("#dualSelectRoles2 option").each(function () {
        usersRoles.push(jQuery(this).val());
    });
    console.log(usersRoles);

    jQuery.ajax({
        type: "POST",
        url: "@Url.Action("AddUser")",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify(usersRoles),
        success: function (data) { alert(data); },
        failure: function (errMsg) {
            alert(errMsg);
        }
    });
}

所有我想要做的就是接受我的JSON对象在我的MVC的行动?

All I want to do is receive my JSON object in my mvc action?

推荐答案

不幸的是字典有问题总是与模型MVC绑定。 这里阅读完整的故事。因此,我们必须创建自己的自定义模型绑定拿到字典作为参数传递给我们的控制器动作。

Unfortunately Dictionary got always problem with Model Binding in MVC. Read the full story here. So we have to create our own custom model binder to get the Dictionary as a parameter to our controller action.

要解决您的要求,这里是工作的解决方案 -

To solve your requirement, here is the working solution -

首先,创建于以下方式你的ViewModels。 PersonModel可以有RoleModels列表。

First create your ViewModels in following way. PersonModel can have list of RoleModels.

public class PersonModel
{
    public List<RoleModel> Roles { get; set; }
    public string Name { get; set; }
}

public class RoleModel
{
    public string RoleName { get; set;}
    public string Description { get; set;}
}

然后请有这将是服务基本指标视图索引操作 -

Then have a index action which will be serving basic index view -

    public ActionResult Index()
    {
        return View();
    }

索引视图将有以下的JQuery AJAX POST操作 -

Index view will be having following JQuery AJAX POST operation -

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    $(function () {
        $('#click1').click(function (e) {

            var jsonObject = {
                "Name" : "Rami",
                "Roles": [{ "RoleName": "Admin", "Description" : "Admin Role"}, { "RoleName": "User", "Description" : "User Role"}]
            };

            $.ajax({
                url: "@Url.Action("AddUser")",
                type: "POST",
                data: JSON.stringify(jsonObject),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (response) {
                    alert(response.responseText);
            },
                success: function (response) {
                    alert(response);
                }
            });

        });
    });
</script>

<input type="button" value="click1" id="click1" />

Index操作职位,以ADDUSER行动 -

Index action posts to AddUser action -

    [HttpPost]
    public ActionResult AddUser(PersonModel model)
    {
        if (model != null)
        {
            return Json("Success");
        }
        else
        {
            return Json("An Error Has occoured");
        }

    }

所以,当发生后,你可以得到在行动的模型参数的所有发布的数据了。

So now when the post happens you can get all the posted data in the model parameter of action.

这篇关于如何接收JSON在MVC 5操作方法为放慢参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆