我想在ASP.net mvc中使用Ajax檢索html表中的數據,但成功部分未執行,並且不知道如何在表中顯示返回的數據使用Ajax。請提出任何方法來解決這個問題。感謝..數據沒有在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>
@Html.TextBox("searchString");
<input type="button" value="filter" id="Button1" />
<table id="showData">
@{Html.RenderPartial("SearchList");}
</table>
</body>
</html>
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());
}
}
SearchList.cshtml
@foreach (var item in Model)
{
<tr>
<td>@item.ProductName</td>
<td>@item.ProductId</td>
<td>@item.ProductDesc</td>
</tr>
}
首先,它是'success'不'Success' –
是它扔了什麼錯誤。它是否在控制器 –
中執行操作,並且在您的ajax調用中還包括'dataType:'json';' –