-1

Goal:
Remove the session of SearchData by pressing the reset button.

Problem:
Is it possible to do it?

Info:
The address "ASP.NET removing an item from Session? "doesn't tell me how to use reset button to remove the session!

HTTPContext.Session["SearchBoat"]



@using (Html.BeginForm("Boat", "Shipments", FormMethod.Post))
{
        <input type="reset" value="Reset" />

        <input type="submit" value="Search" />
}
Community
  • 1
  • 1
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145

1 Answers1

1

Your reset button, is a Form Reset button, that will only empty your form fields, if you want that button to be pressed as well and actually post a form, you have several ways:

way 1 (2 forms):

@using (Html.BeginForm("Boat", "Shipments"))
{
    <input type="submit" value="Search" />
}
@using (Html.BeginForm("Reset", "Shipments"))
{
    <input type="submit" value="Reset" />
}

way 2 (add name and check that):

@using (Html.BeginForm("Boat", "Shipments"))
{
    <input type="submit" name="submitButton" value="Search" />
    <input type="submit" name="submitButton" value="Reset" />
}

public ActionResult Shipments(FormCollection form) 
{
    if(form["submitButton"] == "Reset") {
        // reset was pressed
        Session.Remove("SearchBoat"); // remove session
        return RedirectToAction("Index"); // redirect
    }

    // submit was pressed
}

way 3 (through an ActionNameSelectorAttribute):

https://stackoverflow.com/a/7111222/28004


P.S. No need to specify FormMethod.Post as that's the default value.

Community
  • 1
  • 1
balexandre
  • 73,608
  • 45
  • 233
  • 342