大家好,我是一名新手ASP MVC程序员。我需要将我的模型作为参数传递给Ajax POST请求。
以下是我的Ajax POST请求代码:
这是模型。
这些是控制器。
当我通过ajax post请求传递我的模型时,
另外,我该如何在我的ajax请求中传递模型和多个int参数?
这是我的视图:
以下是我的Ajax POST请求代码:
<script type="text/javascript">
$(document).ready(function () {
$("#contragentTable tr").click(function () {
$.ajax({
type: 'POST',
url: "/Contragent/Index",
data: $('#form').serialize(),
dataType: 'json'
});
});
});
</script>
这是模型。
public class ContragentModel
{
private readonly List<ContragentView> contragentList = new List<ContragentView>();
public ContragentModel()
{
this.IsRowSelected = false;
}
public List<ContragentView> ContragentList
{
get
{
return this.contragentList;
}
}
public ContragentView SelectedContragent { get; set; }
public bool IsRowSelected { get; set; }
}
这些是控制器。
public ActionResult Index()
{
var contragentModel = new ContragentModel();
var contragentView = new ContragentView();
contragentView.Id = 1;
contragentModel.ContragentList.Add(contragentView);
return View(contragentModel);
}
[HttpPost]
public ActionResult Index(ContragentModel model)
{
model.IsRowSelected = true;
// Here exception throws, because there no items
model.SelectedContragent = model.ContragentList.Single(t=>t.Id== 1);
return this.RedirectToAction("Index", model);
}
当我通过ajax post请求传递我的模型时,
model.ContragentList
没有项目。然而在cshtml端它有。有人知道为什么吗?另外,我该如何在我的ajax请求中传递模型和多个int参数?
这是我的视图:
@model Transportation.Models.ContragentModel
@{
ViewBag.Title = "";
Layout = "~/Views/Shared/_MainLayout.cshtml";
}
@section head{
<script type="text/javascript">
$(document).ready(function () {
$("#contragentTable tr").click(function () {
$.ajax({
type: 'POST',
url: "/Contragent/Index",
data: $('#form').serialize(),
dataType: 'json',
contentType: 'application/json; charset=utf-8'
});
});
});
</script>
}
<table id="contragentTable" class="table table-hover table-bordered">
<tr id="0" style="background-color: #ccc">
<th>
@Html.ActionLink("some text1", "Index")
</th>
<th>
@Html.ActionLink("some text2", "Index")
</th>
<th />
<th></th>
</tr>
@if (@Model.ContragentList.Count > 0)
{
<tr id="@index.ToString()">
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
}
else
{
<tr>
<td colspan="9">No data
</td>
</tr>
}
</table>
<div>
@{ var displayStyle = @Model.IsRowSelected ? "" : "none";
var operationTypeGroups = Model.IsRowSelected ? Model.SelectedContragent.PriceList.GroupBy(t => t.OperationTypeId) : null;
var operationTypes = operationTypeGroups == null ? null : operationTypeGroups.SelectMany(t => t);
<table id="priceTable" class="table table-hover table-bordered" style="display: @displayStyle">
<tr id="0" style="background-color: #ccc">
<th>
</th>
<th>
</th>
@if (operationTypes != null)
{
foreach (var operationType in operationTypes)
{
<th>
@Html.ActionLink(operationType.OperationTypeName, "Index");
</th>
}
}
<th></th>
</tr>
</table>
}
</div>
application/x-www-form-urlencoded
。而且,您不应该使用{ model: serializedform }
。唯一的要求是具有正确的输入字段名称。这就是为什么我要求OP展示他的视图以查看他如何生成相应的输入字段。不幸的是,他似乎没有回应,所以我们只能在这里猜测并提出建议,而不能提供有建设性的答案。 - Darin Dimitrov