Reputation: 43
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!!!
Upvotes: 0
Views: 1657
Reputation: 7605
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.
Upvotes: 0
Reputation: 1903
You should pass Model to the partial View
In your View
@model List<Mvc4Web.Models.imovel>
@Html.Partial("TesteLista",Model)
Upvotes: 3