-1

net mvc5 form and Using checkbox.

My condition is i need to Call HTTPPOST action immedaitely after selecting the checkbox(true)

Its something like HTTPPOST action should be called immediately after selecting the checkbox.

I need to pass Model as well to the HTTP Post.

can you please let me know whats the desired way to get this done ?

1
  • Using razor you can use @Html.CheckBox("chkName",false, new {onclick="this.form.submit();"}) - see here Commented Oct 6, 2017 at 14:37

2 Answers 2

0

I'd recommend using jquery to achieve this. Assuming your checkbox is nested in a form, you can use:

$(function () {
    $('#CheckBoxName').change(function () {
        $(this).closest("form")[0].submit();
    });
});

This will trigger a postback, calling the appropriate HttpPost method and sending the model as a parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

it helped to fix the issue
0

You should use javascript to react on a change of the checkbox then posting via ajax.

Here is an exemple :

$('#checkbox').change(function(){
    if (this.checked){
                var jsonModel = '@Html.Raw(Json.Encode(Model))';

                $.ajax({
                type: 'post',
                url: '@Url.Action("UrlOfPostAction")',
                data: { Model: jsonModel},
                dataType: 'json',
                success: function (res) {
                    //Do something
                }
                //manage errors
            });
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.