Reputation: 754
Hi Im new to ASP and MVC3 and my question is how can i clear the button name parameter in my Post because everytime I refreshes my page after clicking the button with the button parameter , the Value of my last clicked button is still there, is there any way to clear this thing up after Submit or when refreshing the page in Controller or by using Jquery?
Thanks
Heres my Code Snippet:
[HttpPost]
public ActionResult HistoryPage(HistoryModel model, string btnAction)
{
if (Session["HistoryId"] != null)
{
switch (btnAction)
{
case "Delete History":
DeleteHistory(model, ref deleteHistoryError, ref deleteHistorySucesss);
break;
case "AddHistory":
AddHistory(model);
break;
}
}
return View(model);
}
Here is the DeleteHistory
private static void DeleteHistory(HistoryModel model, ref string ErrorMessage, ref string SuccessMessage)
{
foreach (var item in model.HistoryIds)
{
if (item != "")
{
bool result = Int32.TryParse(item, out HistoryIds);
if (result)
{
var History= db.History.Find(HistoryId);
bool HistoryExist = true;
if (History.HistoryId != null)
{
History.LogicalDelete = true;
History.DateModified = DateTime.Now;
db.SaveChanges();
SuccessMessage = "History successfully deleted";
}
else
{
ErrorMessage = "Unable to delete History.";
}
}
}
}
if (!string.IsNullOrWhiteSpace(ErrorMessage))
{
SuccessMessage = String.Empty;
}
}
}
}
my Cshtml Button below my form
<input type="submit" name="btnAction" class="btnMultiDelete button_example button_example_small div-bottom-3 w100Percent txtAlignLeft"
value="Delete History" id="btnDeleteHistory" />
Upvotes: 0
Views: 119
Reputation: 17182
On browser refresh (clicking on F5), Browser issues the last request which was made (in your case it is post
). This is the default behavior of the browser.
So we have to follow PRG
pattern here. PRG - POST-REDIRECT-GET
. so in your code instead of returning view, you have to return RedirectToAction("Get Action Name")
. In this case, the last request for the browser would be GET
and when you do a subsequent refresh, it will issue a GET
request instead of POST
.
Your code should be something like this -
[HttpPost]
public ActionResult HistoryPage(HistoryModel model, string btnAction)
{
if (Session["HistoryId"] != null)
{
switch (btnAction)
{
case "Delete History":
DeleteHistory(model, ref deleteHistoryError, ref deleteHistorySucesss);
break;
case "AddHistory":
AddHistory(model);
break;
}
return RedirectToAction("Get Action Name ...");
}
return View(model);
}
Upvotes: 1