我正在尝试第一次使用开源Kendo Grid。我有了基本的网格并运行得很好,但是现在我需要添加一个搜索功能,并搜索名字和姓氏。我试图做到这一点在阿贾克斯,但我被困在了一个错误:向Kendo Grid添加文本搜索
错误:无法调用方法的不确定
我的代码“读”:
<div id="search-index">
<div class="editor-field">
<label>First Name:</label>
@Html.TextBox("FirstName")
<label style = "margin-left: 15px;">Last Name:</label>
@Html.TextBox("LastName", "", new { style = "margin-right: 15px;" })
</div>
<div id="search-controls-index">
<input type="button" id="searchbtn" class="skbutton" value="Search" />
<input type="button" id="addPersonbtn" class="skbutton" value="Add New Person" onclick="location.href='@Url.Action("AddPerson", "Person")'"/>
</div>
</div>
<div id="index-grid"></div>
</div>
$(document).ready(function() {
var grid = $('#index-grid').kendoGrid({
height: 370,
sortable: true,
scrollable: true,
pageable: true,
dataSource: {
pageSize: 8,
transport: {
read: "/Home/GetPeople",
dataType:"json"
}
},
columns: [
{ field: "FirstName", title: "First Name" },
{ field: "LastName", title: "Last Name" },
{ field: "Gender", title: "Gender" },
{ field: "DOB", title: "Date of Birth", template: '#= kendo.toString(new Date(parseInt(DOB.substring(6))), "MM/dd/yyyy") #' },
{ field: "IsStudent", title: "Is a Student?" }]
});
$("#searchbtn").on('click', function() {
var fsname = $("#FirstName").val();
var ltname = $("#LastName").val();
$.ajax({
type: 'GET',
url: '@Url.Content("~/Home/GetPeople")',
data: { fname: fsname, lname: ltname },
success: function (data) {
grid.dataSource.read();
},
error: function() {
$("#index-grid").html("An error occured while trying to retieve your data.");
}
});
});
});
应该的问题,但这里是我的控制器(采用asp MVC 3):
public JsonResult GetPeople(string fname, string lname)
{
if (((fname == "") && (lname == "")) || ((fname == null) && (lname == null)))
{
var peopleList = repo.GetPeople();
return Json(peopleList, JsonRequestBehavior.AllowGet);
}
else
{
var personResult = repo.GetSearchResult(fname, lname);
return Json(personResult, JsonRequestBehavior.AllowGet);
}
}
它是否对GetPeople进行初始页面加载调用? – Myzifer 2013-05-01 09:04:30
仅供参考,您可以使用'string.IsNullOrWhitespace(fname)'来检查空,空字符串或空白。不需要手动进行所有那些检查......非常混乱。 – 2014-08-02 04:51:36