I have some problem passing a Model from a Controller to a View. The model is something like this:
public class ClassA
{
public ClassA()
{
this.List1 = new List<ClassB>();
}
public int Value { get; set; }
public List<ClassB> List1 { get; set; }
}
Where ClassB
is for instance:
public class ClassB
{
public string something { get; set; }
}
In the Controller, I have
using CQC.Models;
using CQC.ViewModels;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.IO;
using System.Linq;
using System.Web.Mvc;
namespace CQC.Controllers
{
public class SampleController : Controller
{
public ActionResult Xyz()
{
ClassA reg = new ClassA();
reg.Value = 1;
reg.List1.Add(new ClassB() { something = "aaa" });
return View(reg);
}
}
}
Debugging and pausing just before the return View(reg)
I confirm that the reg
contains what I want.
So, in the View ( and using the @model CQC.ViewModels.ClassA
) I can see the Model.Value
but the list Model.List1
is empty.
Please, what am I missing?
The View is like that:
@model CQC.ViewModels.ClassA
@{ ViewBag.Title = "abc"; }
<fieldset>
<legend>Detail</legend>
<div class="row">
<div class="label">@Html.LabelFor(model => model.Value)</div>
<div class="output">@Html.DisplayFor(model => model.Value)</div>
</div>
@if (Model.List1.Count > 0)
{
<div class="rowAutoHeight">
<div class="label">@Html.Label("List1")</div>
<table class="stripe hover" id="in_tbl1">
<thead>
<tr>
<th>Programa</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.List1)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Something)</td>
</tr>
}
</tbody>
</table>
</div>
}
</fieldset>