2

I make a page in asp.net MVC .In this page exist a View and a PartialView.I put button into partialview and write Countinue function into View.

button:

    <button type="button" class="btn btn-warning center-block btn-large" id="Continue" onclick="Continue(this);">Continue</button>

js:

 var Continue=function (e) {
            e.preventDefault();
            if ($('#FieldsetCharterFlightsReturn article').size() < 1) {
                if (validateDeparturef(e)) {
                    $("#CharterFlightsForm").submit();
                }

            }
                        if ($('#FieldsetCharterFlightsReturn article').size() > 0) {
                if (validateDeparturef(e) && validateArrivalf(e)) {
                    $("#CharterFlightsForm").submit();
                }
            }
        };

Now When i click on the button get this error:

TypeError: Continue is not a function

What is right way to bind event of elements into PartialView?

6
  • Have your properly included the Continue function definition in the page ? Also you are passing this which will be the button and it will not have a method called preventDefault() Commented Aug 25, 2016 at 12:36
  • Yes.I can delete e.preventDefault() Commented Aug 25, 2016 at 12:37
  • Is it in the same view ? or an external js file ? can you put a console.log("This function exist"); just above your function definition, run your page and verify that message appear in the browser console. Commented Aug 25, 2016 at 12:39
  • I wrote java script codes into script tag into view. Commented Aug 25, 2016 at 12:41
  • Try one like function Continue() also you need to pass event like onclick="Continue(event);" Commented Aug 25, 2016 at 12:43

2 Answers 2

3

Since you are using bind event using it instead of ugly-inline click handler.

 $(function(){
    $('#Continue').on('click', Continue);
 })

OR, Using Event delegation

 $(document).on('click', '#Continue', Continue);

And remove inline onclick="Continue(event);" handler

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

1 Comment

OK.If i don't use ajax for get partialveiw This works currently. Hove ever I found a another way that replace button tag with a tag and delete preventDefault() function.In this way I can use inline code and this work always. Thanks a lot :)
2

Use following pattern:

<button type="button" class="btn btn-warning center-block btn-large" id="Continue" onclick="module.Continue(event);">Continue</button>

(function($, module) {

    module.Continue=function (e) {
        e.preventDefault();
        if ($('#FieldsetCharterFlightsReturn article').size() < 1) {
            if (validateDeparturef(e)) {
                $("#CharterFlightsForm").submit();
            }

        }
        if ($('#FieldsetCharterFlightsReturn article').size() > 0) {
            if (validateDeparturef(e) && validateArrivalf(e)) {
                $("#CharterFlightsForm").submit();
            }
        }
    };

}(jQuery, module= window.module|| {}));

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.