ASP.NET MVC AJAX将参数传递给Controller

时间:2017-07-15 07:01:21

标签: javascript c# asp.net ajax asp.net-mvc

我收到此错误

  

没有任何论据符合所需的形式参数' SSN' HomeController.GetCICO(string)

来自此代码:

public JsonResult GetAllCICO()
    {
        var cicos = GetCICO().ToList();
        var jsonResult = Json(new{data = cicos}, JsonRequestBehavior.AllowGet);
        jsonResult.MaxJsonLength = int.MaxValue;
        return jsonResult;
    }

这是GetCICO:

public List<CICO> GetCICO(string SSN)
{
List<CICO> cicos = new List<CICO>();
using (SqlConnection con = new SqlConnection())
{
    con.ConnectionString = str;
    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.Connection = con;
        cmd.CommandTimeout = 180;
        cmd.CommandText = "SELECT * FROM source_ips WHERE ssn_or_tin = '"+SSN+"' ORDER BY dateTrans ASC";
        con.Open();
        using (SqlDataReader sdr = cmd.ExecuteReader())
        {
            if (sdr.HasRows)
            {
                while (sdr.Read())
                {
                    CICO cico = new CICO()
                    {
                        ssn_or_tin = sdr["ssn_or_tin"] == DBNull.Value ? (double?)null : Convert.ToDouble(sdr["ssn_or_tin"]),
                        cusid = sdr["cusid"] == DBNull.Value ? (double?)null : Convert.ToDouble(sdr["cusid"]),
                        accountNo = sdr["accountNo"] == DBNull.Value ? (double?)null : Convert.ToDouble(sdr["accountNo"]),
                        dateTrans = sdr["dateTrans"].ToString(),
                        transCode = sdr["transCode"] == DBNull.Value ? (int?)null : Convert.ToInt32(sdr["transCode"]),
                        transdescription_1 = sdr["transdescription_1"].ToString(),
                        amount = sdr["amount"] == DBNull.Value ? (double?)null : Convert.ToDouble(sdr["amount"]),
                        cashin = sdr["cashin"] == DBNull.Value ? (double?)null : Convert.ToDouble(sdr["cashin"]),
                        cashout = sdr["cashout"] == DBNull.Value ? (double?)null : Convert.ToDouble(sdr["cashout"]),
                        source = sdr["source"].ToString()
                    };
                    cicos.Add(cico);
                }
            }
        }
        con.Close();
    }
}
return cicos;
}

这是我的Javascript:

var SSNdata = { SSN: $("#SSN").val() };
        $.ajax({
            type: "POST",
            url: "/Home/GetCICO",
            data: SSNdata,
            dataType: "json"
        });

4 个答案:

答案 0 :(得分:0)

那么,您要发布哪种方法?我想这就是你想要的。

JS:

url: "/Home/GetAllCICO"

C#:

[HttpPost]
public JsonResult GetAllCICO(string SSN)
{
    var cicos = GetCICO(SSN).ToList();
    var jsonResult = Json(new{data = cicos});
    return jsonResult;
}

答案 1 :(得分:0)

好像你在控制器中调用了错误的动作&#34; GetCICO&#34;你需要致电&#34; GetALLCICO&#34;在您的网址中:

url: "/Home/GetCICO"  this is wrong URL 

Your URL should be 

url: "/Home/GetALLCICO"

你的Json Action应该是:

    [HttpPost]
    public JsonResult GetAllCICO(string SSN)
    {
        var cicos = GetCICO().ToList();
        var jsonResult = Json(new{data = cicos}, JsonRequestBehavior.AllowGet);
        jsonResult.MaxJsonLength = int.MaxValue;
        return jsonResult;
    }

因为您是一个参数&#39; SSN&#39;在您的网址中:

var SSNdata = { SSN: $("#SSN").val() };
$.ajax({
        type: "POST",
        contentType: "application/json;charset=utf-8",
        url: "/Home/GetAllCICO",
        data: JSON.stringify(SSNdata),
        dataType: "json"
    });

根据这个你的控制器动作应该是:

[HttpPost]
public JsonResult GetAllCICO(string SSN)

答案 2 :(得分:0)

Hansmagz,我认为您正试图通过 HttpGet 来获取uri &#34; / Home / GetAllCICO&#34; 的数据?

如果是,请尝试以下代码。

jquery的

var SSNdata = { SSN: $("#SSN").val() };
    $.ajax({
        type: "GET",
        url: "/Home/GetCICO",
        data: SSNdata,
        dataType: "json"
    });

MVC控制器

public JsonResult GetAllCICO(SSNdata data)
{
    var cicos = GetCICO(data.SSN).ToList();
    var jsonResult = Json(new{data = cicos}, JsonRequestBehavior.AllowGet);
    jsonResult.MaxJsonLength = int.MaxValue;
    return jsonResult;
}

SSNdata类

public class SSNdata
{
     public string SSN{get;set;}
}

希望这有帮助!

答案 3 :(得分:0)

Javascript Code ::

                var ssnData="123";
                $.ajax({
                                type: "POST",
                                contentType: "application/json;charset=utf-8",
                                data: JSON.stringify({ 'SSNdata': ssnData }),
                                url: '@Url.Action("GetAllCICO", "Home")',
                                dataType: 'json',
                                success: function (cicos) {
                                    //cicos is your response data
                                }
                            });
    Controller part::

            public JsonResult GetAllCICO(string SSNdata)
                    {
                        var json=Json("Here your list",JsonRequestBehavior.AllowGet);
                        return json;
                    }