Bob Cichon
Bob Cichon

Reputation: 209

Cannot display complex viewmodel to view - Data is there, just wont display

I have a complex viewModel made up of a simple model and another complex model.

I can see the data for all parts gather and pass to the view. It is there under

@html>ViewData>Model.

Both complete sets in the view. But when I render the page, the values are all null on the Model.Compeditor.CompeditorId side as example

View Code:

        @model eManager.Web2.Models.CombineCompClass

    @{
        ViewBag.Title = "Competitor's Detail";
    }

    <h2>@ViewBag.Title</h2>

<fieldset>
    <legend>Compeditor</legend>
    <table>
        <tr>
            <td>


 <div class="display-field">
               @Html.HiddenFor(model => Model.Compeditor.CompeditorId)
            </div>

                <b>First Name</b>
                <div class="display-field">
                    @Html.DisplayFor(model => Model.Compeditor.FirstName)

                </div>
            </td>
            <td>
            <b>Last Name</b>
            <div class="display-field">
                @Html.DisplayFor(model => Model.Compeditor.LastName)

View Model Code:

    namespace eManager.Web2.Models
{
    public class CombineCompClass
    {
        private Core.Compeditor comp;
        private IQueryable<Models.AddCompToEventClass> classlist;

        public CombineCompClass(Core.Compeditor comp, IQueryable<Models.AddCompToEventClass> classlist)
        {
            this.comp = comp;
            this.classlist = classlist;
        }
       public AddCompToEventClass AddCompToEventClass { get; set; }
       public Compeditor Compeditor { get; set; }

    }
}

Controller to show: Although the data is gathering and passing to the model as expected

public ActionResult CompeditorDetail(int CompeditorId)
    {
        var comp = _db.Compeditors.Single(c => c.CompeditorId == CompeditorId);

        var classlist = from o in _db.Events
                    join o2 in _db.Event_Classes on o.EventID equals o2.EventID
                    where o.EventID.Equals(o2.EventID)
                    join o3 in _db.Class_Definitions on o2.ClassID equals o3.Class_Definition_ID
                    where o2.ClassID.Equals(o3.Class_Definition_ID)
                    where o.CurrentEvent.Equals(true)

                    select new AddCompToEventClass
                    {
                        Event = o,
                        Event_Class = o2,
                        Class_Definition = o3,
                    };

        var model = new CombineCompClass(comp, classlist);


        return View(model);
    }

I have a single simple set of data and then a list.

First I try to get the simple data to display. I can get the Model.Compeditor.CompeditorId to reference through intellesence, but again no data is there.

I think I just need to change how the display is seen, but I cannot figure out how to see it.

Ok.. I just saw how both the models are null and the data is in comp and classlist as I have defined these in the Controller. So the model for Compeditor and AddCompToEventClass are null after pass and the values are in comp and classlist. Is this the wrong way to do it in the controller?

Ok I think I found my issue... was passing the below objects as well... null and they where effecting the model:

public AddCompToEventClass AddCompToEventClass { get; set; }
       public Compeditor Compeditor { get; set; }

Removed and data is now accessible.. now on to @foreach issues with one of the sets!

Upvotes: 0

Views: 69

Answers (1)

Andy T
Andy T

Reputation: 9881

Rather than:

@Html.HiddenFor(model => Model.Compeditor.CompeditorId)

Try changing the case of "Model" to "model":

@Html.HiddenFor(model => model.Compeditor.CompeditorId)

Upvotes: 2

Related Questions