Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 从视图向控制器发送动态列表_Asp.net Mvc_Asp.net Mvc 5 - Fatal编程技术网

Asp.net mvc 从视图向控制器发送动态列表

Asp.net mvc 从视图向控制器发送动态列表,asp.net-mvc,asp.net-mvc-5,Asp.net Mvc,Asp.net Mvc 5,当我添加/删除行时,得到了相当多的响应 加载页面时,将添加默认行3。(但用户可以删除这些行) 当我尝试添加新行时,控制器中会出现4个值行,但当我尝试删除任何默认行时,它不会显示新添加的行。 下面是我的代码 @model ProductRegistration.Models.InstalledProductInformationModel @using (Html.BeginForm()) { @for (int i = 0; i < ((Model != null &&am

当我添加/删除行时,得到了相当多的响应

加载页面时,将添加默认行3。(但用户可以删除这些行) 当我尝试添加新行时,控制器中会出现4个值行,但当我尝试删除任何默认行时,它不会显示新添加的行。 下面是我的代码

@model ProductRegistration.Models.InstalledProductInformationModel
@using (Html.BeginForm())
{
    @for (int i = 0; i < ((Model != null && Model.listInstallProducts != null) ? Model.listInstallProducts.Count : 3); i++)
    {
        <tr>
            <td>
                @Html.TextBoxFor(m => m.listInstallProducts[i].SerialNumber, new { @class = "form-control input-sm input-Serial", @placeholder = "Enter serial number", @maxlength = 50, @OnKeypress = "return alphanumeric_only(event);" })
                @Html.ValidationMessageFor(m => m.listInstallProducts[i].SerialNumber)
                <div class="SerialErrorDisplay" style="color: red"></div>
            </td>
            <td>
                @Html.DropDownListFor(m => m.listInstallProducts[i].ModelNumber, new SelectList(string.Empty, "Value", "Text"), "--Select Model--", new { @class = "form-control input-sm input-Model" })
                @Html.ValidationMessageFor(m => m.listInstallProducts[i].ModelNumber)
                <div class="ModelErrorDisplay" style="color: red"></div>
            </td>
            <td>
                @Html.TextBoxFor(m => m.listInstallProducts[i].InstallationDate, new { @class = "form-control input-sm input-Date", @maxlength = 50, @placeholder = "DD/MM/YYYY" })
                @Html.ValidationMessageFor(m => m.listInstallProducts[i].InstallationDate)
                <div class="DateErrorDisplay" style="color: red"></div>
            </td>
            <td>
                <img src="~/Images/delete_icon.png" onclick="DeleteProduct(this);" />
            </td>
        </tr>
    }
    <input class="btn btn-primary top-padding" type="submit" value="Previous" id="back-step" name="direction"/>
}
模型为:

public class InstalledProductInformationModel : InstalledProductInformation
{
    public InstalledProductInformation installedProductInformation { get; set; }
    public List<InstalledProductInformation> listInstallProducts { get; set; }
}
公共类InstalledProductInformation模型:InstalledProductInformation { 公共InstalledProductInformation InstalledProductInformation{get;set;} 公共列表listInstallProducts{get;set;} }
请帮帮我。

列表索引是连续枚举的,因此不会有间隙。如果删除一行,列表索引将不再连续。如果要处理索引,新行应该在那里。

默认情况下,
DefaultModelBinder
要求集合索引器从零开始,并且是连续的。如果删除第一个项目(indexer=0),则不会绑定任何项目。如果删除第三项(indexer=2),则仅绑定前两项。您可以通过为名为
Index
的特殊属性添加输入来覆盖此行为,其中值等于索引器

@for (int i = 0; i < ((Model != null && Model.listInstallProducts != null) ? Model.listInstallProducts.Count : 3); i++)
{
  <tr>
    <td>
      @Html.TextBoxFor(m => m.listInstallProducts[i].SerialNumber, new { @class = "form-control input-sm input-Serial", @placeholder = "Enter serial number", @maxlength = 50, @OnKeypress = "return alphanumeric_only(event);" })
      @Html.ValidationMessageFor(m => m.listInstallProducts[i].SerialNumber)
      <div class="SerialErrorDisplay" style="color: red"></div>
    </td>
    ....
    <td>
      // Add the following input
      <input type="hidden" name="listInstallProducts.Index" value="@i" /> 
      <img src="~/Images/delete_icon.png" onclick="DeleteProduct(this);" />
    </td>
  </tr>
}
<input class="btn btn-primary top-padding" type="submit" value="Previous" id="back-step" name="direction"/>
@for(int i=0;i<((Model!=null&&Model.listInstallProducts!=null)?Model.listInstallProducts.Count:3);i++)
{
@Html.TextBoxFor(m=>m.listInstallProducts[i].SerialNumber,新{@class=“表单控制输入sm输入序列号”,@placeholder=“输入序列号”,@maxlength=50,@OnKeypress=“仅返回字母数字(事件);”})
@Html.ValidationMessageFor(m=>m.listInstallProducts[i].SerialNumber)
....
//添加以下输入
}

旁注:在将模型传递到视图之前,您应该在控制器中添加3项,以便获得正确的模型绑定,然后只需使用
@for(int i=0;i

表示。。。??你能再多给我一点吗?或者你能给我发一些参考链接吗?看看你的表格代码。按照您给出的Razor代码,表单输入应该包含索引。第一个文本框将是listInstallProducts[0],第二个文本框将是listInstallProducts[1],依此类推。当您添加新行并删除其中一个默认行(比如第3行)时,您的表单中有索引0、1和3,索引2缺失。但请稍候,列表中的索引不能在C#中有间隙。这就是为什么您的模型绑定器只将第0行和第1行绑定到列表中,并保留第4行。Stephen Muecke已经给出了答案-hidden属性
Index
。你可以在上找到更多,但我有另一个问题。这里我验证所需的字段属性。因此,当我输入新行并单击提交按钮时,验证不会显示。但如果我们点击submit,它可以再次验证(显示必需)。如何实现这一点?这是一个不同的问题,因此您需要提出一个新问题(但在添加动态内容时需要重新分析验证器)。例如:你能再解释一下吗。。请解释是哪一位,我的答案还是重新对验证器进行分析(注意我添加了链接)?这里我们没有使用“unobtrusive.js”。我们仅使用服务器验证进行验证
@for (int i = 0; i < ((Model != null && Model.listInstallProducts != null) ? Model.listInstallProducts.Count : 3); i++)
{
  <tr>
    <td>
      @Html.TextBoxFor(m => m.listInstallProducts[i].SerialNumber, new { @class = "form-control input-sm input-Serial", @placeholder = "Enter serial number", @maxlength = 50, @OnKeypress = "return alphanumeric_only(event);" })
      @Html.ValidationMessageFor(m => m.listInstallProducts[i].SerialNumber)
      <div class="SerialErrorDisplay" style="color: red"></div>
    </td>
    ....
    <td>
      // Add the following input
      <input type="hidden" name="listInstallProducts.Index" value="@i" /> 
      <img src="~/Images/delete_icon.png" onclick="DeleteProduct(this);" />
    </td>
  </tr>
}
<input class="btn btn-primary top-padding" type="submit" value="Previous" id="back-step" name="direction"/>