2

My current requirement is to show a warning message to the user after the form is submitted for the first time. When the user clicks the submit button next time, it should be saved as the user can ignore the warning message.

I am showing the warning message using a session variable and setting the count to "1" to show the warning message.

The issue is when the user refreshes the page, the form is getting submitted as the viewmodel is being passed to the action. I do not want the refresh to submit the form.

Since the session variable is 1 after first time, the refresh is also picking up the same view model as the submit action.

Please help if there is a way to differentiate between the refresh and the submit action in MVC.

Vikram
  • 19
  • 1
  • 2
  • You have to submit the same form twice? Not just show a confirmation dialog? – JamieD77 Apr 10 '17 at 20:39
  • Yes, I need to show the same form twice.I cannot show them a dialog box. – Vikram Apr 10 '17 at 20:42
  • See if this helps you http://stackoverflow.com/questions/2126747/how-to-supress-re-post-when-refreshing-a-page-asp-net-mvc – JamieD77 Apr 10 '17 at 20:46
  • Thanks Jamie for the reply. I looked into this and was running into an issue. In my scenario, the user does not need to do anything to correct the warning and can be ignored. After step 4 in this pattern, if the user does a refresh, the form is getting re-submitted. 1. GET “Products/Create” 2. User types in some information 3. POST “Products/Create” 4. Validation fails, re-display the form with warnings – user corrects the input 5. POST “Products/Create” – Vikram Apr 11 '17 at 12:55

1 Answers1

0

It really depends on what the warning is for, is the warning checking for something or is the warning always there by default?

If it is always there by default the easiest solution would be to have the warning appear via some jquery the first time where a fake submit button is pressed and have the actual submission button enabled and appear when the warning appears with the fake button disappearing. So on the page in the html hide and disable the submit button with display: none and disabled initially and place a fake button in the same place, then something like this in the js:

$('#yourformidhere').submit(function () {
            $('#fakesubmitbutton').hide();
            $('#realsubmitbutton'.show();
            $(this).find(':submit').prop('disabled', 'false');
        });

If you have to check something server side then you need to return the page and model via the controller. On the page use a hidden marker set to say '1' and then in the controller perform the check and return the page if the check fails with the model field with the marker changed to '2' along with the warning. The user can then submit the form again, your controller action checks for the '2' and if it finds it allows for submission of the form without the check.

Edit - An example:

A form:

@using (Html.BeginForm()){

@Html.Hiddenfor(model => model.hidden)

@Html.EditorFor(model = model.data1)

<input type ="submit" value"submit"/>
}

Viewmodel:

public class someviewmodel{

public string data1 { get; set; }
public int hidden { get; set; }

}

Controller

public ActionResult forumsubmit(someviewmodel model)
        {

//check to see if this is the first submission attempt

if (model.hidden == null){

      //check for whatever you need regarding the warning e.g.

      if (model.data1 == needswarning){

      //add your warning message here if it is to appear on the page for example a viewbag
      ViewBag.warning = "you've been warned"

     //change hidden field to 1 to acknowledge first submission attempt
     model.hidden = 1;

     //returns view with information intact for second submission
     return View(model);
    }

}

//if you reach here in the controller action the hidden field is either not null or there is no need for a warning so you can now process the form and add info to db here.

return View("submitsuccess")

}

Rob
  • 199
  • 21
  • I have to perform validations on the server side.Here are the steps I am doing. 1. Create/Student. Hidden field is set to null. 2. User types some information. 3. Post the “Student/Create/StudentViewModel” 4. Set hidden field to 1. 5. The View is shown with the warning message. 6. Post the form Student/Create/StudentViewModel.(Without changing any information on the form, the data should be saved) The issue is between steps 5 and 6. When the user refreshes the page, the form is getting submitted as the user does not need to change anything. The form passes the ViewModel on refresh. – Vikram Apr 11 '17 at 13:16
  • The controller dictates whether the form is submitted or not, so your controller is your method of control. On first submit the hidden field is null, so in the controller we perform the check for the warning and with it we check to see if hidden field = null. If hidden field = null the form does not submit and instead the controller returns the page with model and hidden field now changed to 1. Please let me know if this isn't clear and i'll try to give you an example. – Rob Apr 11 '17 at 14:36
  • added an example to answer – Rob Apr 11 '17 at 15:21
  • Thanks Rob for the example. I tried it. After the first submit, the controller sends the hidden value as 1 to the view and the warning shows correctly. When the form is submitted again, the value of hidden again is becoming null. – Vikram Apr 11 '17 at 15:49
  • I don't think I understand the problem you are having. When the page is returned the hidden value will be 1 on the form so when you submit the form the second time the hidden value will be submitted as 1. What exactly do you mean by the value of hidden 'becoming null'? – Rob Apr 12 '17 at 16:06
  • Thanks Rob for your help. I got this resolved using the http://stackoverflow.com/questions/8748940/hiddenforx-x-id-is-being-populated-by-the-urlparameter-instead-of-the-viewm . The issue I had was with the Modelstate. – Vikram May 12 '17 at 15:11