views:

231

answers:

2

this is the master page :

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />

    <asp:ContentPlaceHolder ID="header1" runat="server" />
</head>

<body>
<div class="page">
    <div id="header">
        <div id="title">
            <h1>My MVC Application</h1>
        </div>

        <div id="logindisplay">
            <% Html.RenderPartial("LogOnUserControl"); %>
        </div> 

        <div id="menucontainer">

            <ul id="menu">              
                <li><%= Html.ActionLink("Home", "Index", "Home")%></li>
                <li><%= Html.ActionLink("About", "About", "Home")%></li>
                <li><%= Html.ActionLink("Imoveis", "Index", "Categoria")%></li>
                <li><%= Html.ActionLink("Admin", "Index", "Admin")%></li>
                <li><%= Html.ActionLink("User", "Index", "User")%></li>
            </ul>

        </div>
    </div>
    <div id="left">

     <% Html.RenderPartial("~/Views/Imovel/Pesquisa.ascx"); %>   

    </div>
    <div id="main">
        <asp:ContentPlaceHolder ID="MainContent" runat="server" />

        <div id="footer">
        </div>
    </div>
</div>
</body>
</html>

Partial View

  <%= Html.DropDownList("categoria_id", (SelectList)ViewData["Categoriass"], "--Selecciona um--")%>



            <div class="editor-label">
            <%= Html.LabelFor(model => model.categoria_id) %>
        </div>
        <div class="editor-field">
            <%= Html.DropDownListFor(model => model.categoria_id, (SelectList)ViewData["Categorias"], "--Selecciona um--")%>

            <%= Html.ValidationMessageFor(model => model.categoria_id) %>
        </div>

This is the problem:

    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";

        **ViewData["Categoriass"] = new SelectList(catRepository.FindAllCategorias().AsEnumerable(), "id", "nome", 3);**



        return View();
    }

Since the partial view is in the master page, how do I get its model?

A: 

You can create base class for your controllers and set it during creation if you need to query for it with every request:

public class BaseController : Controller
{
    public BaseController()
    {
        var catRepository = ...;
        ViewData["Categoriass"] = new SelectList(catRepository.FindAllCategorias().AsEnumerable(), "id", "nome", 3);
    }
}

That is not really efficient,because it will be executed with every controller. You can also create action filter which sets ViewData and apply it where needed.

LukLed
+1  A: 

I think you should create an ActionFilter and apply it on your controllers.

Create an action filter like this

public class DataForMasterPageAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //initiate your repository

           var catRepository = ...;

        //then create the viewdata like so

        filterContext.Controller.ViewData["Categorias"] = new SelectList(catRepository.FindAllCategorias().AsEnumerable(), "id", "nome", 3);
    }

}

Then apply it on the controller and it will be available for all actions as well. Like so;

[DataForMasterPage]
public class CategoriaController : Controller
{
      public ActionResult Index()
      {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            return View();
      }
}

On the partial view you just call the ViewData as usual, no need to change anything

<div class="editor-label">
        <%= Html.LabelFor(model => model.categoria_id) %>
    </div>
    <div class="editor-field">
        <%= Html.DropDownListFor(model => model.categoria_id, (SelectList)ViewData["Categorias"], "--Selecciona um--")%>

        <%= Html.ValidationMessageFor(model => model.categoria_id) %>
    </div>

Might have performance issues, but its one of the simplest ways to avoid setting the ViewData on every method.

Nick Masao