Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know that using .submit will trigger an event when the submit button is clicked. But I would like to trigger the event when the submit button have been clicked and the page reloaded (I think every page reloads after a form have been submitted).

I have this form

HTML
@using (Html.BeginForm("Feedback", "Home", FormMethod.Post))
                    {
                        @Html.ValidationSummary()

@Html.ValidationSummary("", new {@class = "text-danger"})
                        <div class="form-group">
                            @Html.LabelFor(m => m.Cell, new {@class = "col-md-2 control-label"})
                            <div class="col-md-10">
                                @Html.EditorFor(m => m.Cell, new {htmlAttributes = new {@class = "form-control", placeholder = "Phone Number", type = "text"}})
                            </div>
                        </div>
<div class="col-sm-6 col-sm-offset-3">
                            <div class="btn-toolbar">
                                <button class="btn-raised btn-success btn">
                                    Submit
                                    <div class="ripple-container"></div>
                                </button>
                                <button class="btn btn-default">Cancel</button>
                            </div>
                        </div>
}


What I have tried:

<script>
jQuery(document)
.ready(function() {

jQuery("#Cell").inputmask("099 999 9999");

jQuery("#Cell")
.on("blur",
function() {
var last = $(this).val().substr($(this).val().indexOf("-") + 1);
if (last.length == 4) {
var move = $(this).val().substr($(this).val().indexOf("-") - 1, 1);
var lastfour = move + last;
var first = $(this).val().substr(0, 9);
$(this).val(first + '-' + lastfour);
}
});

// check if user just submitted this page
var justSubmitted = $.cookie('just_submitted');
if (justSubmitted) {
$("#feedback").show();
// delete the cookie
$.cookie('just_submitted', null);
}

// submited
$("form")
.submit(function() {
// create a cookie, then let it submit normally
$.cookie('just_submitted', 'true');
});


});


</script>
Posted
Updated 27-Jul-16 4:57am

1 solution

Check with this once.
Action Method:
public JsonResult SaveData(string UName)
{
//saving logic
return Json(new { UName, url = Url.Action("AnotherView", "Employee") });

}

View:
<input type="text" id="UserName" />

<input id="btnSave" type="submit" name="Save" value="Save" />
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script type="text/javascript">
$("#btnSave").click(function(){
$.ajax({
url: '@Url.Content("/Employee/SaveData")',
type: 'POST',
data: { UName: $('#UserName').val() },
cache: false,
success: function (data) {
alert('data inserted successfully with UserName : ' + data.UName);
//redirection
window.location = data.url;
},
error: function (data) {
alert("error");
}
});
});
</script>

Let me know if any changes required...!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900