0
votes

I've had some problems when I try to render a PartialView.

My Controller:

public ActionResult Index()
    {
        var db = new fanganielloEntities();
        List<imovel> imoveis = (from s in db.imovel
                                where s.StatusImovel == 3
                                select s).ToList();

        return PartialView(imoveis);
    }

     public ActionResult Listar()
     {
         return View();
     }

The View:

 @Html.Partial("TesteLista")

The Partial:

@model List Mvc4Web.Models.imovel
    @if (Model != null)
    {
foreach (var item in Model)
{
            @Html.DisplayFor(modelItem => item.DescricaoImovel)
 }
    }

The Error:

Object reference not set to an instance of an object.

Source Error:

Line 5: Line 6: Line 7: @foreach (var item in Model) Line 8: { Line 9:

Thank in advanced!!!

2

2 Answers

3
votes

You should pass Model to the partial View

In your View

 @model List<Mvc4Web.Models.imovel>
@Html.Partial("TesteLista",Model)
0
votes

Html.Partial will not fire your controller action. If you want to fire the Index action when TesteLista is rendered, use

@Html.Action("TesteLista") 

instead.