Oleg Sh Oleg Sh - 1 year ago 126
jQuery Question

ASP.NET MVC Set property of model during form send

I have 2 submit buttons on ASP.NET MVC form:

@using (Html.BeginForm("ApplicationDriverForm", "ApplicationDriver", routeValues: new { CompanyUniqueString = Model.CompanyUniqueString }, method: FormMethod.Post, htmlAttributes: new { id="appForm1", name="appForm1" }))
{

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" name="btnSave" id="btnSave" value="Save - but don't send" class="btn btn-default" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" name="btnSend" id="btnSend" value="Send" class="btn btn-default" />
</div>
</div>


I want to set different parameters for one of model property. I try to do it using the following way:

<script type="text/javascript">
jQuery(document).ready(function () {
$('btnSave').on('click', function () {
ev.preventDefault();
$('input[[email protected](model=>model.Status)]').val('@Domain.StaticStrings.ApplicationDriverStatusStrings.Processing');
if ($(this).valid())
$('#appForm1').submit();
});

$('btnSend').on('click', function () {
ev.preventDefault();
$('input[[email protected](model=>model.Status)]').val('@Domain.StaticStrings.ApplicationDriverStatusStrings.Submit');
if ($(this).valid())
$('#appForm1').submit();
});
});
</script>


but nothing happened, these functions are not call at all, form is sending with empty Status property. Why?

Answer Source

You're calling ev.preventDefault(), but ev is undefined, because your handler functions don't take the event as a parameter. Change it to:

$('btnSave').on('click', function (ev) {

And

$('btnSend').on('click', function (ev) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download