使用jquery post将Dictionary数据传递到Controller字符串方法 [英] Pass the Dictionary data to Controller string method using jquery post

查看:130
本文介绍了使用jquery post将Dictionary数据传递到Controller字符串方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Jquery代码在下面给出

My Jquery code is given below

 $("#btnExec").live('click', function () {
       var data1= "lorem ipsum";
       var data2= "lorem ipsum";
       var data3= "lorem ipsum";
       var dict = new Object();

       $("#ArgsTable tr").each(function (e) {
                var firstTD = $(this).find("td").eq("0").html().trim().replace(/\s/g, "");
                var secondValue = $(this).find("td").eq("1").find("input:text").val();
                dict[e] = [firstTD,secondValue]; 
       });

       $.ajax({
                type: 'POST',
                cache: false,
                data: { strdata1: data1, strdata2: data2, strdata3: data3, myDictionary: dict },    
                url: '<%=Url.Action("ExecData","Home") %>',
                success: function (data) {        
                }
              });
 });

下面给出了我的控制器方法

And my controller method is given below

public string ExecData(string strdata1, string strdata2, string strdata3,  Dictionary<object, object> myDictionary)
{
    //do some stuff...
}

如果我单击 btnExec ,它会正确触发字符串值的控制器方法,但是字典值始终为 null ..

If i click btnExec means, its fired the controller method with string values properly, but the dictionary values come always null ..

在我的场景中,控制器方法的返回类型应仅是字符串"

In my scenario "the return type of controller method should be string only"

我该如何解决?提前致谢 !!!

How can i solve this? Thanks in advance !!!

推荐答案

Tyr像这样:

[HttpPost]
public ActionResult ExecData(
    string strdata1, 
    string strdata2,  
    string strdata3, 
    Dictionary<string, string> myDictionary
)
{
    return Content("hello world");
}

然后:

var data = {
    strdata1: 'lorem ipsum',
    strdata2: 'lorem ipsum',
    strdata3: 'lorem ipsum'
};

$('#ArgsTable tr').each(function (index, item) {
    var firstTD = $(this).find('td').eq(0).html().trim().replace(/\s/g, '');
    var secondValue = $(this).find('td').eq(1).find('input:text').val();
    data['myDictionary[' + index + '].Key'] = firstTD;
    data['myDictionary[' + index + '].Value'] = secondValue;
});

$.ajax({
    url: '<%=Url.Action("ExecData","Home") %>',
    type: 'POST',
    traditional: true,
    data: data,
    success: function (result) {

    }
});

这篇关于使用jquery post将Dictionary数据传递到Controller字符串方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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