MVC4 Partial View Error “Object reference not set

2019-02-28 15:00发布

问题:

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

[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);


    }

回答1:

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

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



回答2:

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



回答3:

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.



回答4:

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);