如何使用“重置”按钮删除会话

时间:2015-06-17 20:15:11

标签: c# asp.net-mvc-4 session

目标:
按重置按钮删除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" />
}

1 个答案:

答案 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,因为它是默认值。