您可以让搜索方法返回相同的局部视图:
public ActionResult JsonSearchProduct(string stringSearched)
{
List<ProductModel> products;
// Search for products...
return PartialView("_ProductSearchList", products);
}
如果将局部视图包裹在
div
,您可以将其html替换为jQuery。
$(function () {
$('#search-products').keyup(function () {
var serachstring = $(this).val();
$.ajax({
url: '/Product/JsonSearchProduct/?stringSearched=' + serachstring,
type: 'get',
datatype: 'json',
success: function (data) {
// data will contain the html of the partial view.
$('div#product-grid').html(data);
}
});
});
});
注意:如果视图中有JavaScript,那么Ajax get请求可以是这样的:
$.get('@Url.Action("JsonSearchProduct", "Product")',
{
stringSearched: searchstring
},
function (data) {
$('div#product-grid').html(data);
}
});