C
C#2y ago
pyrodistic

Get Parameter From URL (MVC)

If I have an a Action parameter I can pass a value to it from the View like " asp-route-origin="Owner" ". However, how can I retrieve the value that's already set in the URL for that parameter? I know I can retrieve the default route id in the controller from the view (1?origin=Owner) like:
var foo = Url.ActionContext.RouteData.Values["id"]; //==1 (considering the parenthesis above)
var foo = Url.ActionContext.RouteData.Values["id"]; //==1 (considering the parenthesis above)
But replacing id by the desired parameter name ("origin") doesn't seem to work. What should I be doing here? I thought that the same way I do:
<a asp-action="Create" asp-route-id="@Url.ActionContext.RouteData.Values["id"]" class="btn btn-primary btn-lg mb-1">Create New</a>
<a asp-action="Create" asp-route-id="@Url.ActionContext.RouteData.Values["id"]" class="btn btn-primary btn-lg mb-1">Create New</a>
I could do:
<a asp-action="Create" asp-route-origin="@Url.ActionContext.RouteData.Values["origin"]" asp-route-id="@Url.ActionContext.RouteData.Values["id"]" class="btn btn-primary btn-lg mb-1">Create New</a>
<a asp-action="Create" asp-route-origin="@Url.ActionContext.RouteData.Values["origin"]" asp-route-id="@Url.ActionContext.RouteData.Values["id"]" class="btn btn-primary btn-lg mb-1">Create New</a>
But origin seems to be null. Thanks for the help.
1 Reply
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View