Skip to main content
added 337 characters in body
Source Link
alex
  • 1.4k
  • 2
  • 31
  • 69

I have an action controller that returns a partial view with a query. In this controller I have a viewbag with another query that I want to pass to the partial view so I can use it in a foreach.

Here is my code:

public ActionResult GetQuizResults(string ProfileResult, short occupationID) var query = from o in db.Occupations where o.Profile.Equals(ProfileResult, StringComparison.InvariantCulture) select new QuestionnaireViewModel { OccupationID = o.OccupationID, OccupationName = o.OccupationName, OccupationDescription = o.OccupationDescription }; QuestionnaireViewModel occDetails = new QuestionnaireViewModel(); ViewBag.OccDetails = occDetails.GetOccupationDetails(occupationID); return PartialView("_QuizResults", query.ToList()); } 

How can I pass ViewBag.OccDetails to the _QuizResults partial view?

In my partial view I have:

@model IEnumerable<Proj.Models.QuestionnaireViewModel> @foreach (var item in ViewBag.OccDetails) { @item.OccupationDescriptionOc } 

But I get the error: "Object reference not set to an instance of an object"

I have an action controller that returns a partial view with a query. In this controller I have a viewbag with another query that I want to pass to the partial view so I can use it in a foreach.

Here is my code:

public ActionResult GetQuizResults(string ProfileResult, short occupationID) var query = from o in db.Occupations where o.Profile.Equals(ProfileResult, StringComparison.InvariantCulture) select new QuestionnaireViewModel { OccupationID = o.OccupationID, OccupationName = o.OccupationName, OccupationDescription = o.OccupationDescription }; QuestionnaireViewModel occDetails = new QuestionnaireViewModel(); ViewBag.OccDetails = occDetails.GetOccupationDetails(occupationID); return PartialView("_QuizResults", query.ToList()); } 

How can I pass ViewBag.OccDetails to the _QuizResults partial view?

I have an action controller that returns a partial view with a query. In this controller I have a viewbag with another query that I want to pass to the partial view so I can use it in a foreach.

Here is my code:

public ActionResult GetQuizResults(string ProfileResult, short occupationID) var query = from o in db.Occupations where o.Profile.Equals(ProfileResult, StringComparison.InvariantCulture) select new QuestionnaireViewModel { OccupationID = o.OccupationID, OccupationName = o.OccupationName, OccupationDescription = o.OccupationDescription }; QuestionnaireViewModel occDetails = new QuestionnaireViewModel(); ViewBag.OccDetails = occDetails.GetOccupationDetails(occupationID); return PartialView("_QuizResults", query.ToList()); } 

How can I pass ViewBag.OccDetails to the _QuizResults partial view?

In my partial view I have:

@model IEnumerable<Proj.Models.QuestionnaireViewModel> @foreach (var item in ViewBag.OccDetails) { @item.OccupationDescriptionOc } 

But I get the error: "Object reference not set to an instance of an object"

Source Link
alex
  • 1.4k
  • 2
  • 31
  • 69

Pass ViewBag to PartialView from action controller that returns that PartialView

I have an action controller that returns a partial view with a query. In this controller I have a viewbag with another query that I want to pass to the partial view so I can use it in a foreach.

Here is my code:

public ActionResult GetQuizResults(string ProfileResult, short occupationID) var query = from o in db.Occupations where o.Profile.Equals(ProfileResult, StringComparison.InvariantCulture) select new QuestionnaireViewModel { OccupationID = o.OccupationID, OccupationName = o.OccupationName, OccupationDescription = o.OccupationDescription }; QuestionnaireViewModel occDetails = new QuestionnaireViewModel(); ViewBag.OccDetails = occDetails.GetOccupationDetails(occupationID); return PartialView("_QuizResults", query.ToList()); } 

How can I pass ViewBag.OccDetails to the _QuizResults partial view?