我有一个问题,我有一个引用为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'的表达式
由于将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++
}