locked
How to reset Session for new request? RRS feed

  • Question

  • User-79977429 posted

    Hi

    in my view, i have a simple input text which user can enter a name then submit. after submit, i must to add this string value to list of all string values which user entered before, and return partial view with new strings via ajax. to hold these string values (as List<string>) in memory, i've used Session object & works correctly. My problem is that if i reload the page, The Session data already exists while i want to reset it's value & give to user to enter new values!

    How to do this?

    thanks in advance.

    Tuesday, April 14, 2020 4:51 PM

All replies

  • User-474980206 posted

    this is probably a poor design (why send to server). but on the get of the page you should clear session.

    note: if you load the page in two tabs, you will see they combine their results.

     

    Tuesday, April 14, 2020 5:10 PM
  • User-79977429 posted

    Do you have an idea instead of Session object?

    For clarify, i have another example. Suppose we want to create new order along with it's orderDetails & then save them to database transactionally.

    In create controller, user fill order informations, then at bottom of view, there is a table which hold orderDetails data that exists in memory & user can add,edit or delete each orderDetail item through modal form.

    Does anybody have solution (or tutorial) to accomplish this task?

    thanks in advance

    Tuesday, April 14, 2020 7:04 PM
  • User-474980206 posted

    I'd use a pending order database. create a key for the order, and include in a form field. have users to pending orders tables. then at login you can list pending orders.  at purchase, the pending order database can update the real ordering system.

    this is a great use case for the saga pattern.

    Tuesday, April 14, 2020 7:30 PM
  • User-79977429 posted

    Thanks

    but i want to hold this data in memory, because i have a lot of entities & can not create pending table for each one! I think there is a way to accomplish this task!

    Tuesday, April 14, 2020 7:34 PM
  • User-474980206 posted

    You can store in memory in the client via writing a SPA application, or on the server if the number of users is small and losing the data on a recycle is not fatal.

    Wednesday, April 15, 2020 2:19 AM
  • User665608656 posted

    Hi, hamed_1983

    If you want to clear the session data, you need to make " Session["Person"] = null;".

    I made an example. More details, you could refer to below code:

    HomeController.cs:

    public ActionResult Index()
            {
                Session["Person"] = null;
                return View();
            }
    
            [HttpPost]
            public ActionResult save([Bind(Include = "Id,Name")] Person person)
            {
                List<Person> test = new List<Person>();
                if (Session["Person"] != null)
                {
                    test = (List<Person>)Session["Person"];
                }
                test.Add(person);
                Session["Person"] = test;
                return View("Index");
            }

    Index.cshtml:

    @model Case12.Models.Person
    <form id="save">
        <div class="form-horizontal">
            <div class="form-group">
                @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-3" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-3" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                </div>
            </div>
        </div>
        </form>
    <button id="add">add</button>
    @section Scripts {
        <script>
            $(function () {
                $("#add").click(function () {
                    $.ajax({
                        url: "/Home/save",
                        type: "POST",
                        data: $('#save').serialize()
                    });
                });
            });
        </script>
    
    }

    Here is the debug result:

    Best Regards,

    YongQing.

    Thursday, April 16, 2020 6:16 AM