Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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
C# 如何在ASP.NET-MVC-5代码中为强类型数据模型列表获取多个值_C#_Razor_Asp.net Mvc 5_Strongly Typed View - Fatal编程技术网

C# 如何在ASP.NET-MVC-5代码中为强类型数据模型列表获取多个值

C# 如何在ASP.NET-MVC-5代码中为强类型数据模型列表获取多个值,c#,razor,asp.net-mvc-5,strongly-typed-view,C#,Razor,Asp.net Mvc 5,Strongly Typed View,我有强类型视图模型,我需要从强类型列表的用户处获取多条“列表”记录。我正在努力用razor代码实现这一点。这是[httpget]方法 视图模型 错误是: 索引超出范围。必须为非负数且小于集合的大小。参数名称:索引 您可以采用以下两种方法之一。如果列表中有固定数量的项,只需使用适当数量的实例对其进行初始化即可。例如: _PropertyRentingPriceModel = new List<PropertyRentingPrice> { new PropertyRenting

我有强类型视图模型,我需要从强类型列表的用户处获取多条“列表”记录。我正在努力用razor代码实现这一点。这是[httpget]方法

视图模型 错误是:

索引超出范围。必须为非负数且小于集合的大小。参数名称:索引


您可以采用以下两种方法之一。如果列表中有固定数量的项,只需使用适当数量的实例对其进行初始化即可。例如:

_PropertyRentingPriceModel = new List<PropertyRentingPrice>
{
    new PropertyRentingPrice(),
    new PropertyRentingPrice(),
    new PropertyRentingPrice()
};

我不得不做一些类似的事情,最后使用了我发现的一篇文章。我会发布我的实现,但我必须对它进行足够的自定义,这样它会混淆您要查找的内容。

您遇到了什么错误。并且您只需要尝试渲染第一个项目的元素。您想编辑集合中的所有项目吗?不,我需要从用户处获取值!!!我得到以下错误;索引超出范围。必须为非负数且小于集合的大小。参数名称:Index当
\u StudentLimitedInfoModel
为空列表时-它不包含任何项!无论如何,使用硬编码索引器都无法正常工作。使用
for
循环-
for(int i=0;im.\u StudentLimitedInfoModel[i].StudentNumber\u UWLID,…)等
如果您只是
新建集合,而没有用任何实际数据填充它们,为什么不会引发异常?
<div>
    <div class="form-group">
        @Html.LabelFor(model => model._StudentLimitedInfoModel[0].StudentNumber_UWLID, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            //@Html.EditorFor(model => model._StudentLimitedInfoModel[0].StudentNumber_UWLID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
            @Html.ValidationMessageFor(model => model._StudentLimitedInfoModel[0].StudentNumber_UWLID, "", new { @class = "text-danger" })*@
        </div>
    </div>
@Html.EditorFor(model => model._StudentLimitedInfoModel[0].StudentNumber_UWLID, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
_PropertyRentingPriceModel = new List<PropertyRentingPrice>
{
    new PropertyRentingPrice(),
    new PropertyRentingPrice(),
    new PropertyRentingPrice()
};
@for (var i = 0; i < Model._PropertyRentingPriceModel.Count(); i++)
{
    @Html.EditorFor(m => m._PropertyRentingPriceModel[i].Foo)
}
<input type="text" name="_PropertyRentingPriceModel[0].Foo" value="" />