我正在建立一个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对象的数组作为控制器动作结果中的参数?
答案 0 :(得分:0)
您可以尝试这样:
<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>