ASP.NET MVC 5 表单不发送 table 行详细信息(使用 jQuery 数据 tables 1.10.15)
ASP.NET MVC 5 form doesn't send table rows details(with jQuery datatables 1.10.15)
我创建的视图的形式包括 table 以及从列表初始化的行,table 设计为 jQuery.datatables 1.10.15.
当我尝试发送表单时 table 没有发送,我在操作控制器的列表参数中得到 null。
查看页面:
@model List<SendMails.ViewModels.ViewModelDetailsFile>
@{
ViewBag.Title = "UpdateItems";
}
@using (Html.BeginForm("UpdateItems", "Home", FormMethod.Post)) {
@Html.AntiForgeryToken()
<table id="DetailsExcelChoose">
<thead>
<tr>
<th class="RightToLeft wideIndexCol">IndexRow</th>
<th class="RightToLeft">InvoiceNumber</th>
<th class="RightToLeft wideIndexCol">NumberClient</th>
<th class="RightToLeft ">NameClient</th>
<th class="RightToLeft wideIndexCol">Amount</th>
<th class="RightToLeft wideIndexCol">Email</th>
<th class="RightToLeft wideIndexCol">ContactPerson</th>
<th class="RightToLeft ">Details</th>
</tr>
</thead>
<tbody name="iObjsUpdate">
@if (Model != null)
{
for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td name="IndexRow">@Model[i].IndexRow</td>
<td name="InvoiceNumber">@Model[i].InvoiceNumber</td>
<td name="NumberClient">@Model[i].NumberClient</td>
<td name="NameClient">@Model[i].NameClient</td>
<td name="Amount">@Model[i].Amount</td>
<td name="Email"><input type="email" id="Email" name="Email" value=@Model[i].Email /></td>
<td name="ContactPerson"><input type="text" id="ContactPerson" name="ContactPerson" value=@Model[i].ContactPerson></td>
<td name="Details">@Model[i].Details</td>
</tr>
}
}
</tbody>
</table>
<div class="form-group row">
<button type="submit" class="btn btn-primary">send</button>
</div>
}
JS文件:
$(document).ready(function () {
var dataTable = $("#DetailsExcelChoose").DataTable();
});
控制器动作:
[HttpPost]
[ValidateAntiForgeryToken]
[Route("UpdateItems")]
public async Task<ActionResult> UpdateItems(List<ViewModelDetailsFile> iObjsUpdate)
{
/// iObjsUpdate is null
}
我读到需要为 asp.net 设置名称属性知道列是什么...
但是我在 google 中的所有搜索都没有成功。
如果我不清楚我想要什么,那么...我想将行详细信息作为对象列表发送到控制器操作(是的,我知道我可以在 ajax 中执行此操作,但我想要这样)
要向控制器发送或 post 行详细信息,您必须在 .cshtml 或 [=13] 上使用 隐藏 字段=]查看页。
试试这个
for (var i = 0; i < Model.Count(); i++)
{
<tr>
@Html.HiddenFor(x => x.Model[i].IndexRow)
@Html.HiddenFor(x => x.Model[i].InvoiceNumber)
@Html.HiddenFor(x => x.Model[i].NumberClient)
@Html.HiddenFor(x => x.Model[i].NameClient)
@Html.HiddenFor(x => x.Model[i].Amount)
@Html.HiddenFor(x => x.Model[i].Email )
@Html.HiddenFor(x => x.Model[i].ContactPerson)
@Html.HiddenFor(x => x.Model[i].Details)
<td name="IndexRow">@Model[i].IndexRow</td>
<td name="InvoiceNumber">@Model[i].InvoiceNumber</td>
<td name="NumberClient">@Model[i].NumberClient</td>
<td name="NameClient">@Model[i].NameClient</td>
<td name="Amount">@Model[i].Amount</td>
<td name="Email"><input type="email" id="Email" name="Email" value=@Model[i].Email /></td>
<td name="ContactPerson"><input type="text" id="ContactPerson" name="ContactPerson" value=@Model[i].ContactPerson></td>
<td name="Details">@Model[i].Details</td>
</tr>
}
我创建的视图的形式包括 table 以及从列表初始化的行,table 设计为 jQuery.datatables 1.10.15.
当我尝试发送表单时 table 没有发送,我在操作控制器的列表参数中得到 null。
查看页面:
@model List<SendMails.ViewModels.ViewModelDetailsFile>
@{
ViewBag.Title = "UpdateItems";
}
@using (Html.BeginForm("UpdateItems", "Home", FormMethod.Post)) {
@Html.AntiForgeryToken()
<table id="DetailsExcelChoose">
<thead>
<tr>
<th class="RightToLeft wideIndexCol">IndexRow</th>
<th class="RightToLeft">InvoiceNumber</th>
<th class="RightToLeft wideIndexCol">NumberClient</th>
<th class="RightToLeft ">NameClient</th>
<th class="RightToLeft wideIndexCol">Amount</th>
<th class="RightToLeft wideIndexCol">Email</th>
<th class="RightToLeft wideIndexCol">ContactPerson</th>
<th class="RightToLeft ">Details</th>
</tr>
</thead>
<tbody name="iObjsUpdate">
@if (Model != null)
{
for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td name="IndexRow">@Model[i].IndexRow</td>
<td name="InvoiceNumber">@Model[i].InvoiceNumber</td>
<td name="NumberClient">@Model[i].NumberClient</td>
<td name="NameClient">@Model[i].NameClient</td>
<td name="Amount">@Model[i].Amount</td>
<td name="Email"><input type="email" id="Email" name="Email" value=@Model[i].Email /></td>
<td name="ContactPerson"><input type="text" id="ContactPerson" name="ContactPerson" value=@Model[i].ContactPerson></td>
<td name="Details">@Model[i].Details</td>
</tr>
}
}
</tbody>
</table>
<div class="form-group row">
<button type="submit" class="btn btn-primary">send</button>
</div>
}
JS文件:
$(document).ready(function () {
var dataTable = $("#DetailsExcelChoose").DataTable();
});
控制器动作:
[HttpPost]
[ValidateAntiForgeryToken]
[Route("UpdateItems")]
public async Task<ActionResult> UpdateItems(List<ViewModelDetailsFile> iObjsUpdate)
{
/// iObjsUpdate is null
}
我读到需要为 asp.net 设置名称属性知道列是什么...
但是我在 google 中的所有搜索都没有成功。
如果我不清楚我想要什么,那么...我想将行详细信息作为对象列表发送到控制器操作(是的,我知道我可以在 ajax 中执行此操作,但我想要这样)
要向控制器发送或 post 行详细信息,您必须在 .cshtml 或 [=13] 上使用 隐藏 字段=]查看页。 试试这个
for (var i = 0; i < Model.Count(); i++)
{
<tr>
@Html.HiddenFor(x => x.Model[i].IndexRow)
@Html.HiddenFor(x => x.Model[i].InvoiceNumber)
@Html.HiddenFor(x => x.Model[i].NumberClient)
@Html.HiddenFor(x => x.Model[i].NameClient)
@Html.HiddenFor(x => x.Model[i].Amount)
@Html.HiddenFor(x => x.Model[i].Email )
@Html.HiddenFor(x => x.Model[i].ContactPerson)
@Html.HiddenFor(x => x.Model[i].Details)
<td name="IndexRow">@Model[i].IndexRow</td>
<td name="InvoiceNumber">@Model[i].InvoiceNumber</td>
<td name="NumberClient">@Model[i].NumberClient</td>
<td name="NameClient">@Model[i].NameClient</td>
<td name="Amount">@Model[i].Amount</td>
<td name="Email"><input type="email" id="Email" name="Email" value=@Model[i].Email /></td>
<td name="ContactPerson"><input type="text" id="ContactPerson" name="ContactPerson" value=@Model[i].ContactPerson></td>
<td name="Details">@Model[i].Details</td>
</tr>
}