MVC/Javascript:如何在表单提交中发布整数数组
MVC/Javascript: how to post an array of integers in a form submission?
在为MVC网页提交表单之前,我想查看控制器的关键字段列表。但是,控制器上的参数为null。我的JavaScript如下所示;
$("#savePropertiesToBeAssigned").click(function (e) {
e.preventDefault();
var $propertyIds = $("#propertyRows").find($(".selectProperty:checked"));
var propertyIds = [];
$.each($propertyIds, function () {
var propertyId = $(this).data("msurvey-property-id");
propertyIds.push(propertyId);
});
var $form = $(this).closest("form")[0];
$form.action += "?PropertyIds=" + propertyIds + "&page=" + GetHiddenField("msurvey-page");
$form.submit();
});
MVC操作是;
[HttpPost]
public async Task<ActionResult> AddFromContract(int[] PropertyIds, int? page)
{
var pageNumber = page.GetValueOrDefault(1);
return RedirectToAction("AddFromContract", new { page = pageNumber });
}
PropertyId错误地显示为null,而页面具有正确的值。编辑-我显示视图以回应评论:
@{
ViewBag.Title = "Add properties from the contract into the survey";
}
@using (Html.BeginForm("AddFromContract", "PropertySurvey", FormMethod.Post))
{
<div id="hiddenFields"
data-msurvey-page="@ViewBag.Page"></div>
<input type="hidden" id="propertyIds" name="propertyIds" />
<fieldset>
<legend>@ViewBag.Title</legend>
@if (ViewBag.PagedList.Count > 0)
{
<section id="PropertyList" style="margin-right: 28px;">
<p>
The properties below have already been added to the contract <b>@SessionObjectsMSurvey.SelectedContract.ContractTitle</b>, but are NOT in this survey
<b>@SessionObjectsMSurvey.SelectedContract.SurveyTitle.</b>
</p>
<table class="GridTbl">
<thead>
<tr>
<th>UPRN</th>
<th>Block UPRN</th>
<th>Address</th>
<th style="text-align: center;">
Select All<br/>
<input type="checkbox" id="selectAll"/>
</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3" style="text-align: right;">Select the properties you want to include in the survey, and click on the Save button.</td>
<td style="text-align: center;">
<button id="savePropertiesToBeAssigned" class="btn-mulalley">
Save
</button>
</td>
</tr>
</tfoot>
<tbody id="propertyRows">
@foreach (var property in ViewBag.PagedList)
{
<tr>
<td>@property.UPRN</td>
<td>@property.BlockUPRN</td>
<td>@property.Address</td>
<td style="text-align: center;">
<input type="checkbox"
name="selectProperty"
class="selectProperty"
data-msurvey-property-id="@property.PropertyId"/>
</td>
</tr>
}
</tbody>
</table>
@Html.PagedListPager((IPagedList) ViewBag.PagedList, page => Url.Action("AddFromContract", new {page}))
</section>
}
else
{
<p>Either no properties have been entered, or all of them have been assigned to the survey.</p>
}
</fieldset>
}
@section scripts
{
@Scripts.Render("~/bundles/page/propertyTransfer")
}
不需要任何javascript来解决这个问题。表单中已经有checkbox元素,如果它们具有正确的name
和value
属性,它们将在POST方法中绑定到模型。
删除<input type="hidden" id="propertyIds" name="propertyIds" />
并将视图更改为
@foreach (var property in ViewBag.PagedList)
{
<tr>
<td>@property.UPRN</td>
....
<td style="text-align: center;">
<input type="checkbox" name="propertyIds" value="@property.PropertyId" />
</td>
</tr>
}
并删除该脚本。
表单提交时,所有选中复选框的值都将发送到控制器,并且由于复选框具有name="propertyIds"
,它们将在POST方法中绑定到您的int[] PropertyIds
。
您还需要将<div id="hiddenFields" data-msurvey-page="@ViewBag.Page"></div>
更改为
<input type="hidden" name="page" value="@ViewBag.Page" />
附带说明:我建议您从使用视图模型开始,而不是使用ViewBag
,包括集合的视图模型,该视图模型将包含属性int ID
和bool IsSelected
,这样您就可以在模型中强烈键入视图。
相关文章:
- 如何使用js将SNAPSHOT内部版本号转换为3位数的整数
- 检测个位数整数时正在转换毫秒
- 限制javascript变量的最小/最大整数
- 如何在 Javascript 中将变量的值从字符串转换为整数
- 正则表达式只允许 x 个整数
- 我想在AngularJS应用程序中创建一个输入数字框,用户不应该在该框上键入十进制数字.(一个整数输入框)
- 如何在Chartjs中制作整数比例
- 减去两个索引不同但值相同的整数
- 使用jquery从整数中减去
- 有效地获取两个区间之间的随机整数
- 为什么我的javascript程序没有将其转换为整数而不是字符串
- 带静态字符e输入的文本框数字和带javascript的负整数
- Javascript-当存在's是一个整数标记
- parseInt()方法如何返回整数
- 由于某种原因,JavaScript整数存储为未定义
- Js char数组将字符串整数视为整数
- 如何翻译MySQL查询's ResultSet转换为整数
- 计算阶乘超过了Javascript中的最大整数限制
- 如何使用 num.toString 和 str.split 查找整数的最后一位数字
- 有可能得到一个大整数实例的自然日志吗