-1

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>
Joao
  • 157
  • 1
  • 2
  • 9

1 Answers1

0

Changing the definition of ClassA model class to:

public class ClassA
{
    public ClassA()
    {
        this.List1 = new List<ClassB>();
    }

    public int Value { get; set; }
    public List<ClassB> List1 { get; set; }
}

When ClassA reg = new ClassA(); is invoked in your Controller, a new List1 in instantiated when instance of ClassA is created, to which you can then Add the values.

So this in the View should get a result:

<ul>
    @foreach (var item in Model.List1)
    {
        <li>@item.something</li>
    }
</ul>
RickL
  • 3,318
  • 10
  • 38
  • 39
  • Oh no, I made a mistake when writing here the class, of course it is public ClassA() and not public List1(). I will edit the question. Sorry – Joao Mar 24 '18 at 18:06
  • Heh, easily done. See if it works now because it did on my machine. – RickL Mar 24 '18 at 18:10
  • Strange... not on mine. In the meantime, I've found a similar question here:https://stackoverflow.com/questions/46601700/pass-entire-model-with-sub-model-list-to-controller?rq=1 I think it's the same problem. – Joao Mar 24 '18 at 18:32