2010-05-10 80 views
3

我有一个接受一个字符串参数的MVC JsonResult方法:MVC JsonResult方法不接受参数

public JsonResult GetDestinations(string countryId) 
    { 
     List<Destination> destinations = new List<Destination>(); 

     string destinationsXml = SharedMethods.GetDestinations(); 
     XDocument xmlDoc = XDocument.Parse(destinationsXml); 
     var d = from country in xmlDoc.Descendants("Country") 
       from destinationsx in country.Elements("Destinations") 
       from destination in destinationsx.Elements("Destination") 
       where (string)country.Attribute("ID") == countryId 
       select new Destination 
       { 
        Name = destination.Attribute("Name").Value, 
        ID = destination.Attribute("ID").Value, 
       }; 
     destinations = d.ToList(); 

     return Json(new JsonResult { Data = destinations}, JsonRequestBehavior.AllowGet); 
    } 

有了一个jQuery方法调用方法:

 //Fetch Destinations 
     $("#Country").change(function() { 
      var countryId = $("#Country > option:selected").attr("value"); 
      $("#Destination").html(""); 
      $("#Resort").html(""); 
      $("#Resort").append($("<option></option>").val(0).html("---Select---")); 
      $.ajax({ 
       type: "POST", 
       traditional: true,      
       url: "/Destinations/GetDestinations", 
       data: "{countryId:'" + countryId + "'}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (msg) { 
        BindDestinationSelect(msg) 
       } 
      }); 
     }); 

然而,JsonResult似乎只接收一个空参数。即使Firebug的显示出一个参数被传递:

JSON countryId “11” 来源 {countryId:'11' }

任何想法?由于

+0

FWIW,'“{countryId:'”+ countryId +“'}”'不是JSON。 JSON需要双引号。当我查看Firebug中的POST请求时,您应该真的使用类似http://www.json.org/json2.js的对象序列化为JSON格式 – R0MANARMY 2010-05-10 15:57:38

回答

1

我认为这个问题是你如何实际传递数据时,你正在做这样的字符串:

data: "{countryId:'" + countryId + "'}", 

当在现实中,你应该使用在客户端的结构;

data: { countryId: countryId }, 

jQuery应该能够正确处理传递id然后。正如你现在正在做的那样,它正试图将JSON发送到服务器,这不是MVC期望的,而是期望带有名称/值对的POST。

您可能还想考虑jQuery Form Plugin,因为它可以将您的Javascript结构序列化为POST数据非常容易

+1

,它正在传递名称/值对。当我调用Web服务时(与调用MVC方法相反),传递参数的方式非常相似。 – StephenLewes 2010-05-10 16:09:47

1

啊,经过多番搜索,我发现它失败的原因。

无关,与畸形的JSON等,而是一个事实,即控制器方法犯规知道会发生什么,如果你试图通过它的JSON值:

http://www.c-sharpcorner.com/Blogs/BlogDetail.aspx?BlogId=863

所以在我的情况,我我刚刚选择传递一个字符串值。

 $("#Country").change(function() { 
      var countryId = $("#Country > option:selected").attr("value"); 
      $("#Destination").html(""); 
      $("#Resort").html(""); 
      $("#Resort").append($("<option></option>").val(0).html("---Select---")); 
      $.ajax({ 
       type: "POST", 
       traditional: true, 
       url: "/Destinations/GetDestinations", 
       data: "countryId=" + countryId, 
       success: function (msg) { 
        BindDestinationSelect(msg.Data) 
       } 
      }); 
0
public JsonResult BindAllData(string Userid) 
    { 

     List<VoteList> list = new List<VoteList>(); 
     var indexlist = db.TB_WebSites.ToList(); 
     int i = 0; 
     var countlist = db.Tb_Votes.ToList(); 
     var VCountList = db.Tb_Votes.ToList(); 
     foreach (TB_WebSites vt in indexlist) 
     { 
      bool voted = false; 
     } 

     return Json(new { List = _list }); 

    } 


    function DataBind() { 
     $("#LoadingDatas").show(); 
     var userid = $("#FBUserId").text(); 
     //alert('Data : ' + userid); 
     var InnerHtml = ""; 
     $.ajax(
      { 
       url: '/Gitex/BindAllData/', 
       type: 'POST', 
       data: { "Userid": userid }, 
       dataType: 'json', 
       async: true, 
       success: function (data) { 
        //alert('Done'); 
        //alert(data.List.length); 
        for (var i = 0; i < data.List.length; i++) { 

      }); 

    } 

试试这个,它为我工作 jQuery函数 成功:函数(目的地)

0

在这里,我建议你来装饰你的行动HttpPost属性 ,如: -

[HttpPost] 
public JsonResult GetDestinations(string countryId) 
{ 
    List<Destination> destinations = new List<Destination>(); 

    string destinationsXml = SharedMethods.GetDestinations(); 
    XDocument xmlDoc = XDocument.Parse(destinationsXml); 
    var d = from country in xmlDoc.Descendants("Country") 
      from destinationsx in country.Elements("Destinations") 
      from destination in destinationsx.Elements("Destination") 
      where (string)country.Attribute("ID") == countryId 
      select new Destination 
      { 
       Name = destination.Attribute("Name").Value, 
       ID = destination.Attribute("ID").Value, 
      }; 
    destinations = d.ToList(); 

    return Json(new JsonResult { Data = destinations}, JsonRequestBehavior.AllowGet); 
}