简体   繁体   中英

Cannot get Controller to build model form query and parameter passed in - multiple models to get data

I have 4 simpler models in the larger model I am using here and doing a complex query with a result set. Works great. I have a parameter that is passed in to complete the update in a new table comprised of all parts.

So, int(passed parameter) comes in as expected from action on last view Data queries as expected from query in controller..

I have built other actions with a single model, no issue.

I have tried creating a new model and then specifically adding to the model and I get a null reference error on the int to the model value: (object null reference error) code not currently there:

var model = new AddCompToEventClass();
model.Compeditor.CompeditorId = compeditorid;

I just cannot figure out how to get everything into the model to pass on to the next view.

controller code: Has compeditorId commented. The compeditorid parameter passes as expected to the controller.

    public ActionResult AddCompToEventClass (int compeditorid)
{
    // ?? model.Compeditor.CompeditorId = compeditorid;
    var model = 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 };

return View(model);

Here is the model (based on 4 other models)

 namespace eManager.Web2.Models
{
    public class AddCompToEventClass
    {
        public Compeditor Compeditor { get; set; }
        public Event Event { get; set; }
        public Event_Class Event_Class { get; set; }
        public Class_Definition Class_Definition { get; set; }
    }
}

updated code: I now get the Competitor and the Query, but only the query is passed into the model... With the competitor in the query I get a Linq exception.

public ActionResult AddCompToEventClass (int compeditorid)
    {
        var Compeditor = new Compeditor();
        Compeditor.CompeditorId = compeditorid;

        var model = 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 };



    return View(model);
    }

I get a null reference error on the int to the model value: (object null reference error)

You are missing another constructor possibily:

var model = new AddCompToEventClass();

// model.Compeditor is most likely null

model.Compeditor = new Compeditor();

model.Compeditor.CompeditorId = compeditorid;

update

Updated Query:

public ActionResult AddCompToEventClass (int compeditorid)
{
  var model = 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,
    };

  model.Compeditor = new Compeditor
  {
    CompeditorId = compeditorid
  };

  return view(model);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM