텍스트 상자의 검색 조건을 기반으로 테이블에 데이터를 표시하려고합니다. 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