简体   繁体   English

$ .post在jQuery中有两个参数

[英]$.post with two params in jquery

Please refer below Scirpt code 请参考下面的脚本代码

var criteriaSubmission = new Object();
        criteriaSubmission['CurrentController'] = "xx";
        criteriaSubmission['CurrentAction'] = "yy";
        criteriaSubmission['CurrentArea'] = "Cp";
var columIndex="[1,2,3,4]"

 var addModel = new Object();

$.post('Fav/Add', { addModel: criteriaSubmission, columns: JSON.stringify(columnsIndex) },
        function (data) {

        });

MVC Action code: MVC动作代码:

public ActionResult Add(SavedReportCriteriaModel addModel,string columns)
        {
//DO Something
    }

columns value are passed to controller action but addModel data is not passing. 将column值传递给控制器​​操作,但未传递addModel数据。 addModel value is coming as NULL. addModel值为NULL。

Model code: 型号代码:

public class SavedReportCriteriaModel
    {

        public string CurrentController { get; set; }


        public string CurrentAction { get; set; }

public string CurrentArea { get; set; }
}

i dont want to change the SavedReportCriteriaModel to add another properties for columns. 我不想更改SavedReportCriteriaModel为列添加其他属性。 i need to have two params in action. 我需要有两个参数在起作用。

why addModel is coming as NULL. 为什么addModel为NULL。 what was the problem in jquery.post code ? jquery.post代码中的问题是什么?

why is this for: 这是为什么:

var addModel = new Object();` 

your addModel is a JSON property, and should be written in json as { "addModel" : ... not assigning it to an object... 您的addModel是JSON属性,应以JSON形式写成{ "addModel" : ...而不是将其分配给对象...

with just a simple mockup from your code, and fixing your type columnsIndex (as the property is called columnIndex (singular) I can pass all values to it: 只需使用代码中的一个简单模型,并修复您的类型columnsIndex (由于该属性称为columnIndex (单数),我可以将所有值传递给它:

在此处输入图片说明

the script page is as: 脚本页面为:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>

    var criteriaSubmission = new Object();
    criteriaSubmission['CurrentController'] = "xx";
    criteriaSubmission['CurrentAction'] = "yy";
    criteriaSubmission['CurrentArea'] = "Cp";
    var columIndex = "[1,2,3,4]";

    //var addModel = new Object();

    $(function() {
        $.post('@(Url.Action("Add"))', {
            "addModel": criteriaSubmission,
            "columns": JSON.stringify(columIndex)
        },
        function (data) {
            alert(data);
        });
    });
</script>

To note: 要注意:

  • $(function() {}) that wraps the $.post call as it's important to only post when jQuery is fully loaded and page is ready $(function() {})包装了$.post调用,因为仅在jQuery完全加载且页面准备就绪时才发布,这一点很重要
  • columsIndex was emend to columIndex in the JSON data passed with the post 在与帖子一起传递的JSON数据中, columsIndex被扩展为columIndex
  • addModel as a variable was commented out addModel作为变量被注释掉
  • post URL is actually created using a MVC helper @(Url.Action("Add") this, in my demo site, will create a link as /Home/Add and I have added the ActionResult in the Home controller 实际使用MVC辅助程序@(Url.Action("Add")创建发布网址,在我的演示站点中,它将创建一个链接为/Home/Add并且我已在Home控制器中添加了ActionResult
  • The JSON properties are wrapped in double quotes " JSON属性用双引号引起来"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM