asp.net - 如何将模型的多个实例以表单形式传递给控制器
问题描述
我正在设置一个 asp.net 表单,它收集来自 5 个发电机的读数。
这是每个生成器的模型
public class GeneratorModel
{
public int Id { get; set; }
public String Name { get; set; }
public float Belasting { get; set; }
public float MaxBelasting { get; set; }
public float KoudeReserve { get; set; }
}
这是我目前的看法
@model XX.Models.GeneratorModel
@{
ViewBag.Title = "Data Entry";
}
@using (Html.BeginForm("Index", "DataEntry", FormMethod.Post))
{
<table>
<tr>Generator 1</tr>
<tr>
<td>Enter Belasting: </td>
<td>@Html.TextBoxFor(m => m.Belasting)</td>
</tr>
<tr>
<td>Enter Max Belasting: </td>
<td>@Html.TextBoxFor(m => m.MaxBelasting)</td>
</tr>
<tr>
<td>Koude Reserve: </td>
<td>@Html.TextBoxFor(m => m.KoudeReserve)</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit Form" /></td>
</tr>
</table>
}
我的控制器是
[HttpPost]
public ActionResult Index(GeneratorModel generatorModel)
{
return Content("Hello");
}
目前我只能输入 1 个生成器的值,但我需要从 5 个生成器中读取。
有没有办法在控制器操作结果中获取一个 GeneratorModel 对象数组作为参数?
解决方案
你可以这样尝试:
<table class="col-md-offset-2 table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Belasting</th>
<th>Max Belasting</th>
<th>Koude Reserve</th>
<th></th>
</tr>
</thead>
<tbody id="GenaratorList">
<tr>
<td>@Html.TextBoxFor(m => m.Name)</td>
<td>@Html.TextBoxFor(m => m.Belasting)</td>
<td>@Html.TextBoxFor(m => m.MaxBelasting)</td>
<td>@Html.TextBoxFor(m => m.KoudeReserve)</td>
<td><input type="button" value="Add" onclick="addGenarator()"/></td>
</tr>
</tbody>
</table>
<input type="submit" value="Submit Form" />
然后添加一些 javascript 以在每次单击添加时追加新行,然后仅在添加必需的编号后单击提交。行的发电机。将您的控制器更改为:
[HttpPost]
public ActionResult Index(List<GeneratorModel> generatorModels)
{
return Content("Hello");
}
以及用于追加新表行的 JS:
<script>
count=0;
function addGenarator() {
var name = $('#Manifest_Quantity').val();
var belasting= $('#Belasting').val();
var mxBelasting= $('#MaxBelasting').val()
var reserve= $('#KoudeReserve').val();
var generator= "generatorModels[" + count + "]";
$('#GenaratorList').append('<tr><td><input id="' + generator + '.Name" name="' + generator + '.Name" type="text" class="form-control" readonly value="' + name + '"></td><td><input id="' + generator + '.Belasting" name="' + generator + '.Belasting" type="text" class="form-control" readonly value="' + belasting + '"></td><td><input id="' + generator + '.MaxBelasting" name="' + generator + '.MaxBelasting" type="text" class="form-control" readonly value="' + mxBelasting + '"></td><td><input id="' + generator + '.KoudeReserve" name="' + generator + '.KoudeReserve" type="text" class="form-control" readonly value="' + reserve + '"></td></tr>');
$('#Name,#Belasting,#MaxBelasting,#KoudeReserve').val('');
count++;
}
</script>
推荐阅读
- python - Scikit 学习多元线性回归和多项式特征的系数顺序
- python - 使用底图和 matplotlib 对某个国家/地区的某些城市进行彩色绘图
- discord - 如果消息包含不和谐 py 的表情符号,如何检查 on_message?
- c - if 语句中的多个条件如何工作
- android - Arraylist 在 android 中为空检查
- maven - 为什么我不能将 aar 文件添加到我们的库项目并在没有 aar 的情况下分发它?
- c# - C#热身基础练习
- javascript - 如何打开本地 Excel 工作表以在电子中单击按钮进行编辑?
- python - 如何修复 ValueError:未发现的数据仍然存在:在日期时间转换中(来自 json) - Flask?
- c# - 从开始到结束的所有数字都将添加到列表框 C#