3

First of I am a complete beginner at MVC. How would I be able to display data from the database in the events table in a partial view if a certain boolean field is true.

This is my partial view:

@model IEnumerable<TheBigEvent.Models.RecommendedEvents>
<table>
 <tr>

  <td>  
     @Html.DisplayNameFor(model => model.Event_Name)
      </td>
      <td>
      @Html.DisplayNameFor(model => model.Event_Date)

   </td>
  </tr>

 <tr>   

      @foreach (var item in Model) {

          <td>  

    @Html.DisplayFor(modelItem => item.Event_Name)
</td>
     <td>
    @Html.DisplayFor(modelItem => item.Event_Date)
         </td>
     }

 </tr>                          
   </table>

This is my controller

public ActionResult _RecommendedEvents()

    {


        var recommendedevents = from Events in db.Database1
                                select Events;

        recommendedevents = recommendedevents.Where(s => s.Recommended.Equals(true));

        return PartialView("_RecommendEvents", recommendedevents);


    }

And the Code for displaying the partialview

 @Html.Partial("_RecommmndedEvents")

This is the error I am receiving

enter image description here

[EDIT]

 public ActionResult _RecommendedEvents(RecommendedEvents model)

    {

        model = new RecommendedEvents();


        var recommendedevents = from Events in db.Database1
                                select Events;


        recommendedevents = recommendedevents.Where(s => s.Recommended.Equals(true));








        return View(model);


    }

4 Answers 4

2
@{
        Html.RenderAction("view","controller")
}

This will go to the given controller and action that has to return a partialview with the correct model

Sign up to request clarification or add additional context in comments.

Comments

1

object reference not set to an instance of an object has always been an un initialized list for me. try initializing recommendedevents before setting it. something like

List<Events> recommendedevents = new List<Events>();

replacing Events with whatever the type is.

the first parameter in Html.Partial is the partial name not the method call. you need to either pass the model to your view thought a view model and pass it to the partial

@Html.Partial("_RecommendedEvents", Model.Events)

or load the partial through an ajax call. see my answer here for an example How do I render a partial form element using AJAX

Comments

0

The @HTML.Partial() function does not go through any controller action to render, it just renders the View's HTML in that spot in the document. And you aren't passing in the IEnumerable<TheBigEvent.Models.RecommendedEvents> to that partial view, so when that partial renders, the Model is null.

Put the IEnumerable<TheBigEvent.Models.RecommendedEvents> object into your main page's View Model, or maybe on something in the ViewBag, and pass it to the partial view at the time you call the Partial method:

@HTML.Partial("_RecommmndedEvents", ViewBag.RecommendedEvents)

In the top-level page's controller action, set that ViewBag.RecommendedEvents just like how you are instantiating it in your controller code above.

8 Comments

Okay so i have put @model IEnumerable<TheBigEvent.Models.RecommendedEvents> in the view. and added the Model to the partial as @Html.Partial("_RecommendedEvents", Model); but its still coming up with the same error
Are you instantiating it in the top-level controller action? The action you posted in your question is not getting executed, so you'll need to call that repo code and pass that object as the model to the top level view. Post that controller action as an edit to your question.
Here you go. Im not sure what you mean by top level controller action
You instantiate the model var in that Action and return it without altering it in any way . I doubt that is correct. Did you mean to return View(recommendedevents);?
Yes I have fixed that now, its still giving me the same error though regardless
|
0

The error means your model is null, PartialView() is used when you are using Ajax, otherwise you can write your code as below :

return View("_RecommendEvents", recommendedevents);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.