0

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" />
8
  • how you posting data to this controller ? Commented Jan 14, 2016 at 6:41
  • @FrebinFrancis Sorry, forgot to include the DeleteHistory Code Snippet Commented Jan 14, 2016 at 6:44
  • Show us your cshtml code.
    – ramiramilu
    Commented Jan 14, 2016 at 6:46
  • @ramiramilu added the button in my cshtml Commented Jan 14, 2016 at 6:58
  • @FrebinFrancis updated question Commented Jan 14, 2016 at 6:58

1 Answer 1

1

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);
    }
4
  • or is it possible to add return RedirectToAction("Get Action Name ..."); to the private function for Delete? will it override the return View(model) Commented Jan 14, 2016 at 7:10
  • 1
    No, that is not a good practice, why would want to do redirect in private or business methods? You have to do redirections in Controller actions.
    – ramiramilu
    Commented Jan 14, 2016 at 7:12
  • just saw that I have Condition if DeleteHistory return value is Error or Success, Added return RedirectionToAction if success, let me test this Commented Jan 14, 2016 at 7:26
  • Yes based on the return value of DeleteHistory you can either return View(model) or do a redirection return RedirectToAction("Action name").
    – ramiramilu
    Commented Jan 14, 2016 at 8:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.