我想根據文本框中的搜索條件在表中顯示數據。我已經實現了它,但不使用Ajax,但不知道如何使用jquery調用控制器方法並更新表數據。請嘗試解決我的問題。謝謝...在ASP.net中使用ajax在表中搜索數據MVC
Index.cshtml
@model IEnumerable<MvcApplication4.Models.tbl_product>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
<title>Index</title>
<script type="text/javascript">
$(document).ready(function() {
$('#Button1').click(function() {
alert("button clicked");
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'Home/Index',
data: "{'searchString':'" + document.getElementById('searchString').value + "'}",
async: false,
Success: function (response) {
alert("Success");
window.location.reload();
},
error: function() { alert("error"); }
});
});
});
</script>
</head>
<body>
@* @using (@Html.BeginForm("Index", "Home"))
{*@
@Html.TextBox("searchString");
<input type="button" value="filter" id="Button1" />
@* }*@
<table id="showData">
@{Html.RenderPartial("SearchList");}
</table>
</body>
</html>
SearchList.cshtml(部分圖)
@foreach (var item in Model)
{
<tr>
<td>@item.ProductName</td>
<td>@item.ProductId</td>
<td>@item.ProductDesc</td>
</tr>
}
HomeController.cs
public class HomeController : Controller
{
//
// GET: /Home/
ProductEntities dbentity = new ProductEntities();
public ActionResult Index()
{
return View(dbentity.tbl_product.ToList());
}
[HttpPost]
public ActionResult Index(string searchString)
{
var query = dbentity.tbl_product.Where(c => c.ProductName.Contains(searchString));
return View(query.ToList());
}
}
我改變了上面的代碼並實現了ajax,但它不工作,表沒有更新。 – user2802591