我想在页面加载后调用我的JsonResult,而不是在提交按钮上。我有两个BeginForm函数。
$("#loadTableForm").ready(function() {
//$.post($(this).attr("action"), $(this).serialize(), function(response) {
});
<%using (Html.BeginForm()) {%>
//data
<%using (Html.BeginForm("LoadTable", "Home", FormMethod.Post, new { id = "loadTableForm" })) {%>
//Table here should be loaded after page is loaded
<% } %>
<% } %>
答案 0 :(得分:2)
您不能拥有另一种形式的表单,即无效的HTML。所以你需要只有一个表单然后使用页面加载函数来进行ajax调用:
$(function() {
// anything in here happens after the page loads
});
答案 1 :(得分:2)
首先创建控制器操作:
[HttpPost]
public JsonResult GetData()
{
IList<Person> people = GetPeople();
return Json(people);
}
接下来,您进行ajax调用以获取json数据:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type:"POST",
url: "<%= Url.Action("GetData") %>",
data: "{}", // pass in data usually
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
// TODO work with this data
}
});
});
</script>