目标:
按重置按钮删除SearchData的会话。
问题:
有可能吗?
的信息:
地址" ASP.NET removing an item from Session?"并没有告诉我如何使用重置按钮删除会话!
HTTPContext.Session["SearchBoat"]
@using (Html.BeginForm("Boat", "Shipments", FormMethod.Post))
{
<input type="reset" value="Reset" />
<input type="submit" value="Search" />
}
答案 0 :(得分:1)
您的reset
按钮是Form Reset按钮,只会清空您的表单字段,如果您想要按下该按钮并且实际发布表单 ,你有几种方法:
方式 1 (2种形式):
@using (Html.BeginForm("Boat", "Shipments"))
{
<input type="submit" value="Search" />
}
@using (Html.BeginForm("Reset", "Shipments"))
{
<input type="submit" value="Reset" />
}
方式 2 (添加name
并检查):
@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
}
方式 3 (通过ActionNameSelectorAttribute
):
https://stackoverflow.com/a/7111222/28004
P.S。无需指定FormMethod.Post
,因为它是默认值。