Question

I have a complex View. It has data from 4 Models. The models are all static and work as expected. I have created a ViewModel to attempt to show just the data needed for this view. It is made up of Competitors and some complex Classes and Events they participate in.

I have made a complex ViewModel. When I walk through the Controller, I can see all three parts being constructed from the ViewModel. Its all there including data. When I try to map the values using Intellesense in the View, it has no way of knowing this data, or has no mapping from the complex ViewModel. Am I doing this right? I have tried several ways to map these values to the View. I think I need to initialize or map the values to the Models derived from, I just cannot figure out how.
Please advise on how to map these values, data elements to the view.

ViewModel:

Compeditor is an from an actual model direct to the DB The rest of the data is gathered from multiple tables and passed to view from controller

namespace eManager.Web2.Models
{
    public class CompDetailPlus

    {
        public CompDetailPlus()
    {
        this.Compeditor = new Compeditor();


    }
        public virtual Compeditor Compeditor { get; set; }
        public virtual IEnumerable<InEventClass> InEventClass { get; set; }
        public virtual IEnumerable<AllEventClasses> AllEventClasses { get; set; }

    }

public class Compeditor
    {
        [Key]
        public virtual int CompeditorId { get; set; }
        public virtual string FirstName  { get; set; }
        public virtual string LastName { get; set; }
        public virtual string MiddleInt { get; set; }
        public virtual string StreetAddress { get; set; }
        public virtual string City { get; set; }
        public virtual string State { get; set; }
        public virtual string PostalCode { get; set; }
        public virtual string EmailAddress { get; set; }
        public virtual string HomePhone { get; set; }
        public virtual string CellPhone { get; set; }
        public virtual double Height { get; set; }
        public virtual double Weight { get; set; }
        public virtual int Age { get; set; }
        public virtual int Event_CompId { get; set; }



    }

    public class InEventClass
    {
        public virtual int EventClassID { get; set; }
        public virtual string ClassName { get; set; }
        public virtual bool IsSelected { get; set; }
    }
   //duplicate to simplify how the second list is pulled and then combined with first      list
    public class AllEventClasses
    {
        public virtual int EventClassID { get; set; }
        public virtual string ClassName { get; set; }
        public virtual bool IsSelected { get; set; }
    }

}

Controller:

public ActionResult CompeditorDetail(int CompeditorId)
        {
            //Pull the Competitor detail for the ID passed in
            var comp = _db.Compeditors.Single(c => c.CompeditorId == CompeditorId);

            //Pull a list of Event-Classes the competitor is already signed up for on current event
            var nlist = (from o in _db.Compeditors
                         join o2 in _db.Event_Class_Compeditors_s on o.CompeditorId equals CompeditorId
                         where o.CompeditorId.Equals(CompeditorId)
                         join o3 in _db.Event_Classes on o2.EventClassID equals o3.EventClassID
                         where o2.EventClassID.Equals(o3.EventClassID)
                         join o4 in _db.Class_Definitions on o3.ClassID equals o4.Class_Definition_ID
                         where o3.ClassID.Equals(o4.Class_Definition_ID)
                         select new InEventClass()
                         {
                             ClassName = o4.Class_Name,
                             EventClassID = o2.EventClassID,
                             IsSelected = true
                         }).ToList();

            //pull a complete list of Event Classes that are avaiaible
            var totallist = (from o in _db.Event_Classes
                             join o2 in _db.Event_Classes on o.ClassID equals o2.ClassID
                             where o.ClassID.Equals(o2.ClassID)
                             join o3 in _db.Class_Definitions on o2.ClassID equals o3.Class_Definition_ID
                             where o2.ClassID.Equals(o3.Class_Definition_ID)
                             join o4 in _db.Events on o.EventID equals o4.EventID
                             where o.EventID.Equals(o4.EventID)
                             where o4.CurrentEvent.Equals(true)
                             select new AllEventClasses()
                             {
                                 ClassName = o3.Class_Name,
                                 EventClassID = o2.EventClassID,
                                 IsSelected = false

                             }).ToList();

            var whatsleft = totallist.Where(eachtotalclass => !(nlist.Any(eachClassIHave => eachClassIHave.EventClassID == eachtotalclass.EventClassID))).ToList();

            var model = new CompDetailPlus { AllEventClasses = whatsleft, Compeditor = comp, InEventClass = nlist };

            return View(model);
        }

View:
(Has to show the Competitor detail and a compound list of Event_Classes they are in) In the view, I cannot see the values for any data.. all error on run and no good for display.

@model IEnumerable<eManager.Web2.Models.CompDetailPlus>

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

<h2>@ViewBag.Title</h2>

<fieldset>
    <legend>Compeditor</legend>
            <table border="1" >
                    <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)

                            </div>
                        </td>


                    @using (Html.BeginForm("CompeditorDetail", "Compeditor", FormMethod.Post))


                    {
                        foreach (var item in Model)
                         {
                           <input type="checkbox" name="MyID" value="@item.AllEventClasses.IsSelected"/> @item.InEventClass.ClassName <br />
                           <input type="hidden" name="CompeditorID" value="@item.InEventClass.CompeditorId" />
                           }
                        }

                    </td>
Was it helpful?

Solution

Your View accepts a model of IEnumerable eManager.Web2.Models.CompDetailPlus which would be fine, but your controller is sending a single eManager.Web2.Models.CompDetailPlus object.

Try changing this in your View

    @model IEnumerable<eManager.Web2.Models.CompDetailPlus>

to this:

    @model eManager.Web2.Models.CompDetailPlus

And change the bottom part of your view so that it's iterating through Enumerable compaosite items inside your model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top