Asp.Net MVC with two views and a URL

Asked

Viewed 119 times

4

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

Navbar on top, that’s okay.

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 which is where the site will even happen is the forms part and such.

So come on. 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 product where it lists the products, it gets the model that is nothing more than an EF result.

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

So let’s imagine that now I switch to addresses, then cars, etc... Tags should continue to appear.

I did all this by Javascript, in the tag view I have a javascrip that arrives another URL (tags), and loads the page with the result.

I didn’t like this solution, but I don’t know how else to do it.

Someone has a better solution?

1 answer

1

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

In this case the action must 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.