State management between Requests

ANB 181 Reputation points
2021-11-01T21:20:16.683+00:00

When using NET MVC a lot of people dont like to use Session, TempData, for saving data between requests.
I need to save a entire model between requests and dont know what would be the best high performance/good practice to do it.

Basically I have a page A containing a form.
I also have a button on this page that sends the customer to Page B.
However when the customer comes back from page B to page A, all the model/form is empty.

Could be saving my model (class) into localStorage on the client a good option ?
What would be the best approach ?

Thank you

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,590 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 30,176 Reputation points Microsoft Vendor
    2021-11-02T09:10:45.71+00:00

    Hi @AgaNeto-3704,
    There are several ways in ASP.NET MVC to maintain data when redirecting from one page to another or in the same page.

    1. Hidden Field 2.Cookies 3.Query String 4.ViewData 5.ViewBag 6.TempData
      I personally think you can use cookies. Cookies are useful when storing small amounts of information on the client side.
      Best regards,
      Lan Huang

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. AgaveJoe 29,776 Reputation points
    2021-11-02T12:52:38.823+00:00

    However when the customer comes back from page B to page A, all the model/form is empty.

    It is difficult to provide design advice without detailed requirements and the application flow. There are many options for persisting state in MVC. It is up to you to evaluate each state management option and pick an option that best fits your design.

    I would store the use's input from Form A in a database when the button is clicked. From there use a route parameter to persist an Id (or whatever parameter data) that allows you to fetch the record from the DB to populate Form A.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.