简体   繁体   中英

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[id=@Html.IdFor(model=>model.Status)]').val('@Domain.StaticStrings.ApplicationDriverStatusStrings.Processing');
            if ($(this).valid())
                $('#appForm1').submit();
        });

        $('btnSend').on('click', function () {
            ev.preventDefault();
            $('input[id=@Html.IdFor(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?

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) {

Change the input type to button instead of submit if you wish to prevent the form submission, do work and then submit.

You are also missing a '#' before your JQuery id selectors eg should be ' #btnSave '.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM