Cezar P
Cezar P

Reputation: 115

How to pass data from one actionmethod to another in ASP.NET Core 6.0 MVC

I'm wondering how I should do this: I get the ID of an item by using the asp-route-id="@item.ID" method.

<a asp-controller="Outfit" asp-action="OutfitRatingOpslaan" 
   asp-route-id="@item.ID" class="btn btn-primary">Beoordelen</a>

This ID needs to be remembered for the view:

public IActionResult OutfitRatingOpslaan(item.ID)
{
    Object obj = Container.GetObject(item.ID)       
}

So the ID of that object, needs to be combined with the value i get after pressing on a submit button in the OutfitRatingOpslaan(Item.ID) view.

I have added a picture on how the view looks like and where the user can enter a value.

enter image description here

This new value + the ID of the object need to be combined and stored inside database.

So the end result should look something like this:

public IActionResult OutfitRatingSave()
{
    Rating rating = ratingContainer.AddRating(item.ID, Value);
}

Does anyone have any ideas ? ;p

Upvotes: 0

Views: 623

Answers (1)

Xinran Shen
Xinran Shen

Reputation: 9973

There are two methods can achieve it.

First method, You can use:

return RedirectToAction("action", "controller", new { id = item.ID});

Then in target action, Just use :

public IActionResult OutfitRatingSave(int id)
{
    Rating rating = ratingContainer.AddRating(id, Value);
}

Second method, You can use TempData["xx"] to pass data between actions, refer to below code:

public IActionResult OutfitRatingOpslaan(item.ID)
{
    TempData["id"] = item.ID
    Object obj = Container.GetObject(item.ID)       
}

public IActionResult OutfitRatingSave()
{
    int id = (int)TempData["id"];
    Rating rating = ratingContainer.AddRating(id, Value);
}

Upvotes: 1

Related Questions