如何在窗体中将模型的多个实例传递给控制器



我正在设置一个 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 个生成器中读取。

有没有办法在控制器操作结果中获取生成器模型对象数组作为参数?

你可以这样尝试:

    <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 以在每次单击添加时附加新行,并且仅在添加所需的 no. 生成器行数后单击提交。将控制器更改为:

[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>

最新更新