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

1

I have the following situation, in my _Layout I'm 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 go back 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 I have it.

    
asked by anonymous 08.11.2016 / 17:04

1 answer

1

The solution: Following as reference RoutConfig

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

It looks 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 am 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 get 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});
    
08.11.2016 / 17:42