无法在parentView asp.net @model中加载List <>

问题描述 投票:0回答:1

我有一个问题,我有一个引用为List的模型和另一个模型,两者都合并在ParentModel类中

Karta_Model

public partial class Karta_Model
    {
        [Key]
        public int? Id { get; set; }
        public string? Login { get; set; }
    }

Ustawienia_Model

   public partial class Ustawienia_Model
    {
        [Key]
        public int Idp { get; set; }
        public TimeSpan NormaDzienna { get; set; }
    }

ParentView

public partial class ParentView
    {
        public List<Karta_Model> Model1 { get; set; }
        public Ustawienia_Model Model2 { get; set; }
    }

我以前在这样的视图中引用过:

@model List<Karta_Model>

现在我尝试这样做

@model ParentView

但是TextBoxFor停止工作

 <td id="td01">@Html.TextBoxFor(m => m[nr_rows].DzMiesiaca, new { @class = "nrs_days", @type = "number", @readonly = true })</td>


                <td id="td01">@Html.TextBoxFor(m => m[nr_rows].DzTygodnia, new { @class = "inputtext", @type = "text", @Value = @day, @readonly = true })</td>

错误:

无法将带有[]的索引应用于类型为'object'的表达式

c# asp.net .net asp.net-mvc asp.net-core
1个回答
2
投票

由于将Model类型更改为ParentView,因此需要一个foreach循环,该循环将在Model.Model1上循环:

@int counter=0;
foreach(var item in Model.Model1)
{
 //Now you loop over the Model properties
 <td id="td01">@Html.TextBoxFor(item[counter].DzMiesiaca, new { @class = "nrs_days", @type = "number", @readonly = true })</td>
 <td id="td01">@Html.TextBoxFor(item[counter].DzTygodnia, new { @class = "inputtext", @type = "text", @Value = @day, @readonly = true })</td>

  //To apply indexing in a foreach loop declare a counter variable and increment it
  counter++
}
© www.soinside.com 2019 - 2024. All rights reserved.