【问题标题】:unable to load List<> in parentView asp.net @model无法在 parentView asp.net @model 中加载 List<>
【发布时间】:2020-02-06 09:16:27
【问题描述】:

我有一个问题,我有一个引用为 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; }
    }

父视图

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】:

由于您将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++
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-17
    • 2015-05-28
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    相关资源
    最近更新 更多