C
C#5mo ago
Gipper

How to pass variable from one View to another View in link?

Ok, so I have this code in my view:
@foreach (var item in Model)
{
@if(Model.Any(utilizadorGrupo=>utilizadorGrupo.UtilizadorId == userID))
{
@if (userID == item.UtilizadorId)
{

<a id="nomeGrupo" href="@Url.Action("verPublicacaoGrupo")?nomeGrupo=@item.Grupo.Nome_grupo"><strong>@item.Grupo.Nome_grupo</strong></a>
}
}
}
@foreach (var item in Model)
{
@if(Model.Any(utilizadorGrupo=>utilizadorGrupo.UtilizadorId == userID))
{
@if (userID == item.UtilizadorId)
{

<a id="nomeGrupo" href="@Url.Action("verPublicacaoGrupo")?nomeGrupo=@item.Grupo.Nome_grupo"><strong>@item.Grupo.Nome_grupo</strong></a>
}
}
}
as you can see, in this view I am including a link to another view, I loop through a collection of objects and would like to pass the value of one of the fields of that object (Nome_grupo) to the view being linked to. I am doing it in the Razor syntax way because it seemed simpler, but I am open to any other way as this one has proved a bit wonky.
10 Replies
Angius
Angius5mo ago
asp-route-{name} In general, use the anchor tag helper
<a asp-controller="GrupoController" asp-action="GetGrupos" asp-route-id="@Model.Grupo.Id">Show Grupo</a>
<a asp-controller="GrupoController" asp-action="GetGrupos" asp-route-id="@Model.Grupo.Id">Show Grupo</a>
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-8.0
Gipper
Gipper5mo ago
Oh, I didnt realize asp-route- was a wildcard prefix, I thought asp-route-id was the only valid attribute lol thanks again ❤️ Hey, but how do I "retrieve" that variable in the destination view being linked?
Pobiega
Pobiega5mo ago
you'd take a parameter at the controller action with that name id in that case
Angius
Angius5mo ago
Depends. Could be a part of the route, could be a query param
[HttpGet("api/grupos/{id}")]
public async Task<IActionResult> GetGrupoById(int id)
[HttpGet("api/grupos/{id}")]
public async Task<IActionResult> GetGrupoById(int id)
Could be a query param
[HttpGet("api/grupos")]
public async Task<IActionResult> GetGrupoById([FromQuery] int id)
[HttpGet("api/grupos")]
public async Task<IActionResult> GetGrupoById([FromQuery] int id)
Gipper
Gipper5mo ago
ok, it's as a part of the route and it seems to be working as intended, I'm passing the id in a ViewData entry to the target view itself which I'm not sure is the perfect way to do it, but I think it seems correct enough speaking of which, @ZZZZZZZZZZZZZZZZZZZZZZZZZ @Pobiega if I have more than one thing to pass as model to the view, generally speaking is it preferable to pass one as model and the rest inside viewdata or to create a viewmodel obj with all the data aggregated and pass that viewmodel obj??
Pobiega
Pobiega5mo ago
viewmodel 10000000*10^123478154781%
Angius
Angius5mo ago
Everything in the model ViewData is basically a Dictionary<string, object> I only ever use it if I have to pass, dunno, the current page title from a page to the layout And I think in the middleware that times the page generation But I wouldn't use it to pass anything but simple primitives, and certainly not when there is a better way
Gipper
Gipper4mo ago
I sometimes have two corresponding primitives and pass them both in a Dictionary through ViewData
Gipper
Gipper4mo ago
stay mad
No description
Gipper
Gipper4mo ago
Jk, thanks for the advice guys
Want results from more Discord servers?
Add your server
More Posts