How to Pass a Complex Model Using RedirectToAction in ASP.NET Core

preview_player
Показать описание
Learn how to successfully pass a complex model with a list property using `RedirectToAction` in ASP.NET Core. Discover the use of `TempData` to handle complex data transfer effectively.
---

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Redirect to action pass complex model

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Pass a Complex Model Using RedirectToAction in ASP.NET Core

In ASP.NET Core, redirecting to another action with a complex model can be a challenging task, especially when it involves properties like lists. If you've encountered issues where your list is empty or values are null after performing a redirect, you're not alone. This post will guide you through the problem and offer a simple yet effective solution to pass complex models successfully.

The Problem

When working with ASP.NET Core MVC, you might have a model structure similar to the following:

[[See Video to Reveal this Text or Code Snippet]]

Here you have a basic model, OrgToChooseFrom. Now, if you have a more complex model that includes this as a list, like so:

[[See Video to Reveal this Text or Code Snippet]]

Attempting to pass SelectCounteragentViewModel to another action using RedirectToAction may lead to unexpected results. Specifically, you might end up with an empty Counteragents list and null values for SelectedOrg. This can be frustrating, especially when the simple model worked perfectly fine. Here's an example of how you might try to use it:

[[See Video to Reveal this Text or Code Snippet]]

The Solution with TempData

The root of the issue is that RedirectToAction cannot carry complex models particularly when they involve lists. However, there's a workaround that is both efficient and straightforward: using TempData. This allows you to temporarily store data that can be retrieved on the redirected action.

Step-by-Step Guide

Store the Complex Model in TempData:

When you want to redirect to another action, serialize the complex model and store it in TempData. Here’s how you can do it:

[[See Video to Reveal this Text or Code Snippet]]

Here, we're creating an instance of SelectCounteragentViewModel, populating it with data, and then serializing it into a JSON string before storing it in TempData.

Retrieve the Complex Model in the Redirected Action:

Next, in the action that you're redirecting to, retrieve this data from TempData and deserialize it back into your model:

[[See Video to Reveal this Text or Code Snippet]]

Here, we’re reading TempData, deserializing the JSON string back to SelectCounteragentViewModel, and then passing it to the view.

Benefits of Using TempData

Simplicity: It avoids the hassles of trying to pass complex objects directly through the redirect.

Persistence: Data stored in TempData persists until it is read or the request ends, making it practical for data that is needed for a single subsequent request.

No Model State Issues: Since this is all done behind the scenes, there are no problematic model states to manage.

Conclusion

Passing complex models through RedirectToAction can be tricky, but with TempData, it's manageable. By following this guide, you can ensure that your application retains the data integrity required between actions, simplifying your coding experience in ASP.NET Core.

Keep this technique in mind for future projects, and you'll find it incredibly useful for managing complex data situations!
Рекомендации по теме
join shbcf.ru