ddd*_*ddd 7 asp.net asp.net-mvc jquery json
我试图将一些文本从文本框传递到控制器,以获得JSON结果
function invokeAction() {
var searchText = $("#SearchTextBox").val();
// Invoke MVC controller action
$.getJSON("/Home/Results/" + searchText, bindResults);
}
Run Code Online (Sandbox Code Playgroud)
如果我在此处发出提醒,我可以看到searchText肯定有一个值,但是当我在此控制器操作上设置一个断点时:
public ActionResult Results(string search)
{
var r = from t in db.Restaurants
where SqlMethods.Like(t.Name, "%" + search + "%") || SqlMethods.Like(t.Postcode, search + "%") || SqlMethods.Like(t.CuisineType.Type, search + "%")
orderby t.Name ascending
orderby t.Rating descending
orderby t.NumOfViews
descending
select t;
return Json(r.ToList());
}
Run Code Online (Sandbox Code Playgroud)
传入的字符串为null,但是当我在调试器中检查http上下文时,我的searchtext是url的一部分.
由于这是null,查询不返回任何结果.
我在这里错过了什么吗?
我从服务中返回json时遇到了一些问题,我没有收到任何回电.事实证明,json格式错误,我能够通过处理普通ajax调用的错误选项来测试并获得这些错误.
$.ajax({
type: "GET",
url: "Home/Results/",
data: { search: searchText },
dataType: "json",
error: function(xhr, status, error) {
// you may need to handle me if the json is invalid
// this is the ajax object
},
success: function(json){
alert( "Data Returned: " + json);
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17621 次 |
最近记录: |