Asp.Net MVC with two views and a URL

4

I made a website that should work with the following setting.

NavBar at the top, this is ok.

Below it I have a container div, it is divided into two parts, the left part is a list of, can be called tags, the left side that is where the site will even happen is the forms part and such.

So let's go. This is my _Layout.cshtml:

<div class="container">
  <div class="row">
    <div class="col-md-3">
      @Html.Partial("tags")
    </div>
    <div class="col-md-9">
      @RenderBody()
      <footer>
        ...
      </footer>
    </div>
  </div>
</div>

When I run the URL:

localhost:8080/aplicacao/produtos

The @RenderBody() shows the view Index.cshtml of product where it lists the products, it receives the model that is nothing more than a result of the EF.

Regardless of what @RenderBody() has @Html.Partial("tags") has to appear on the side, but its content is always changing, and important to say, it does not change based on RenderBody() view.

So let's imagine that I now change to addresses, then cars, etc ... The tags should continue to appear.

I have done all this for JavaScript, in the tags view I have a javascrip that arrives another URL (of tags), and loads the page with the result.

I did not like this solution, but I do not know how I would do otherwise.

Does anyone have a better solution?

    
asked by anonymous 07.05.2015 / 02:54

1 answer

1

Depending on how you are loading the tags via javascript, I do not see why they change. But there is another way to do it. It would be using @Html.Action("action", "controller") instead of @Html.Partial("tags")

In this case the action should return a PartialView. Ex.

Controller

public class TagController : Controller
{
    [ChildActionOnly] //Caso você queira impedir que a action seja acessível via url.
    public ActionResult Listar()
    {
         var tags = MetodoParaCarregarAsTags();
         return PartialView(tags);
    }
}

View:

<div class="container">
  <div class="row">
    <div class="col-md-3">
      @Html.Action("Listar", "Tag")
    </div>
    <div class="col-md-9">
      @RenderBody()
      <footer>
        ...
      </footer>
    </div>
  </div>
</div>
    
15.07.2015 / 10:05