2016-01-13 65 views
0

我有一个简单的web应用程序,它有一个HouseUnitController,其操作返回JSONResult在ASP.NET中从JSON返回的未定义数据

public JsonResult GetHouseUnits() 
    { 
     var houseUnits = db.HouseUnits.Include(h => h.HouseModel).Include(h => h.Site) 
      .Select(h => new { 
        h.Block, 
        h.Lot, 
        h.HouseModel.ModelName, 
        h.Site.SiteName, 
        h.IsSold, 
        h.FloorArea, 
        h.LotArea, 
        h.Price 
     }); 
     return Json(houseUnits, JsonRequestBehavior.AllowGet); 
    } 

在我看来,我有:

<button data-retrieve data-view="tree" data-service = "./GetHouseUnits" data-container="#view" class="btn btn-warning btn-sm" > <span class="glyphicon glyphicon-arrow-down"></span>&nbsp; &nbsp;<strong>Load Data </strong>&nbsp;</button> 

当我浏览localhost:62516/HouseUnits/GetHouseUnits,我可以看到返回的JSON结果。但是,当我点击按钮时,我没有收到任何数据。这里是正在为其他HTML页面我的脚本文件:

$('button[data-retrieve]').click(function(){ 
    var treeHeaderLimit = 2;  
    var APIurl = $(this).data('service'); 
    var view = $(this).data("view"); 
    var dataCount = 5; 
    var selId = $(this).data("container"); 

    createView(APIurl, selId, view, dataCount, treeHeaderLimit); 

}); 

我有一种感觉,它是与在按钮的URL,因为它似乎是从AJAX返回的数据是undefined

function createView(APIurl, selId, viewType, dataCount, treeHeaderLimit){ 
$.ajax({ 
    url: APIurl, 
    type: "GET", 
    contentType: "application/json", 
    dataType: "json", //This is used to avoid `No-Access-Control-Allow-Origin` header error 
    success:function(data){ 

     if (viewType=="tree"){ 
      generateTree(data, selId, dataCount, treeHeaderLimit) 
     } else{ 
      generateTable(data, selId, dataCount); 
     } 

    }, 
    error: function (err) { 
     alert(err); 
    } 
}); 

} 

有人可以帮我解决这个问题吗?我试过./GetHouseUnits../GetHouseUnits,但它不起作用。谢谢。

+1

尝试使用Url.Action()代替静态URL的 –

+0

您是否为数据服务属性尝试了'HouseUnits/GetHouseUnits'? – Sunil

+0

谢谢@FrebinFrancis。发布这个答案,我可以接受它。 – Cyval

回答

2

在ASP.NET MVC

,而不是data-service = "./GetHouseUnits"使用Url.Action()方法替换您的静态URL的"@Url.Action("{action}","{controller}")"

希望这有助于

0

的网址应在../Controller-Name/Action-Name.格式 将data-service="./GetHouseUnits"更改为data-service="../Controller-Name/Action-Name"。 为了确保您使用的是正确的网址,只需在动作中放置一个断点即可。 如果您使用的是正确的网址,则会显示相应的操作以进行迭代,如果不使用该操作,则网址错误。

+0

试过那个,没有工作。 – Cyval

+0

您是否检查过断点方法 – anand