c# – 如何在ajax post请求中传递模型?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何在ajax post请求中传递模型?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3682字,纯文字阅读大概需要6分钟。
内容图文
![c# – 如何在ajax post请求中传递模型?](/upload/InfoBanner/zyjiaocheng/827/40ad9759a84443ef9f6beec4219353ff.jpg)
每一个人.
我是asp mvc的新手.我需要在ajax post请求中传递我的模型作为参数.
这是我的ajax帖子请求代码:
<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请求模型中传递我的模型时.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>
解决方法:
请查看文章:http://www.codeproject.com/Articles/678591/CRUD-Operations-using-Partial-V
在本文中,CRUD操作是使用ASP.NET MVC 4 Application中的jQuery AJAX调用执行的.
关于您的代码,您需要修改以下行:
$("#contragentTable tr").click(function () {
var modelDataJSON = '@Html.Raw(Json.Encode(Model))';
$.ajax({
url: "/Contragent/Index",
type: 'POST',
data: { myObject1: modelDataJSON},
dataType: 'json'
});
});
您必须在AJAX调用中为对象提供一个名称,它应该与目标控制器操作方法中的参数名称相同,并且因为您从客户端发送JSON所以操作方法应该是这样的:
public ActionResult Index(string myObject1 )
然后在内部操作中,您可以反序列化JSON对象并创建模型或您需要的任何处理.
内容总结
以上是互联网集市为您收集整理的c# – 如何在ajax post请求中传递模型?全部内容,希望文章能够帮你解决c# – 如何在ajax post请求中传递模型?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。