My form has two "save"-buttons - one for saving and redirecting to the Index view, and one for saving and returning to the Edit view.
With mouse click operation it works as it should, but I want to be able to press CTRL+S to save and redirect back to the Edit view.
This is the Edit form:
<form asp-action="Edit" id="myForm">
<!-- more form items -->
<button type="submit" name="save" value="">
Save and go to index
</button>
<button type="submit" name="save" value="stay">
Save and stay here
</button>
</form>
This is the controller method:
public async Task<IActionResult> Edit(int id, string save, [Bind("form fields")] Model model)
{
// save form data and stuff
if (save == "stay") // "save" is the name of the submit buttons
{
return View(auto.Map<ViewModel>(model));
}
else
{
return RedirectToAction("Index");
}
}
This is the key handling jQuery:
$(window).bind('keydown', function(event) {
if (event.ctrlKey || event.metaKey) {
if (String.fromCharCode(event.which).toLowerCase() == 's') {
event.preventDefault();
$("[name=save]").val('stay');
$("#myForm").submit();
}
}
});
As you can see, I am setting the value for the submit-button to "stay", in an attempt to get returned to the Edit-view after save, as per my controller method logic, but I get redirected to the Index-view anyway.
If I mouseclick the "stay"-button, the value "stay" is received by the controller. If I keypress CTRL+S, it is not.