How do I get the view id if I have Asp.net mvc?

Asked

Viewed 882 times

1

I have the following situation, in my _Layout I am checking:

@{
    var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
    var view = HttpContext.Current.Request.RequestContext.RouteData.Values["action"].ToString();
 }

I send this information to the controller so that after changing the language of the page I can return to it.

<a href="@Url.Action("AlteraIdioma", "Home" , new{LinguagemAbreviada="pt", NomeControler = @controller, NomeView= @view })" >
    <img src="~/ContentAdmin/dist/img/brasil-160x160.png" id="pt" class="user-image" alt="brasil">
    <span class="hidden-xs">-</span>
</a>

I want to know how I can get the "Id" if you have.

  • It wouldn’t be easier to get the full url?

  • can try using viewBag

  • @Eduardosampaio, I needed an example, thank you

1 answer

1

The solution is: Following as reference RoutConfig

defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

It was like this in the view:

@{
    var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
    var view = HttpContext.Current.Request.RequestContext.RouteData.Values["action"].ToString();
    var id = HttpContext.Current.Request.RequestContext.RouteData.Values["id"];  
 }

I’m sending the data to the controller like this:

<a href="@Url.Action("AlteraIdioma", "Home" , new{LinguagemAbreviada="pt", NomeControler = @controller, NomeView= @view, IdPagina=@id })" >
    <img src="~/ContentAdmin/dist/img/brasil-160x160.png" id="pt" class="user-image" alt="brasil">
    <span class="hidden-xs">-</span>
</a>

I receive the data:

public ActionResult AlteraIdioma(string LinguagemAbreviada, string NomeControler, string NomeView, string IdPagina)

Redirect to the same page:

return RedirectToAction(NomeView, NomeControler, new { id=IdPagina});

Browser other questions tagged

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