Why is Ajax being called multiple times.

3 posts, 1 answers
  1. Answer
    Greg Gum
    Greg Gum avatar
    18 posts
    Member since:
    Jun 2009

    Posted 12 Dec 2013 Link to this post

    I have a Window which has an Ajax Form with a button.  The first time submit is pressed, it calls the controller once as expected.  However, the second time the button is pressed, the controller is called 3 times. The third time the button is pressed, it is getting called 8 times.

    So something is clearly wrong.  The Ajax call replaces the div (which has the button) so it appears the button is getting wired up multiple times, once for each ajax call. 
    @(Html.Kendo().Window()
        .Name("loginWindow") //The name of the window is mandatory. It specifies the "id" attribute of the widget.
        .Title("Login") //set the title of the window
         
        .Content(@<text>
         @Html.Partial("_LoginPartial")
         </text>)
        .Draggable() //Enable dragging of the window
        .Width(300)
        .Height(350)
        .Modal(true)
        .Visible(false)
                )
    This is the partial view that is called.
    @model NTC.PropertySearch.Models.LoginModel
     
    @{
        ViewBag.Title = "Log in";
    }
     
    <section id="loginWindow">
     
        @using (Ajax.BeginForm("Login", "Account", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "loginSection", }))
        {
            <div id="loginSection">
     
                @ViewBag.Title
                 
                <div >
                <p>
                     @Html.DisplayFor(m => m.Message)
                </p>
                    <p>
                        @Html.DisplayFor(m => m.NumberOfLoginAttempts)
                    </p>
                </div>
                 
                 
        <table>
                     
            <tr>
                <td>@Html.LabelFor(m => m.UserName)
     
                </td>
     
            </tr>
     
            <tr>
     
                <td>
                    @Html.TextBoxFor(m => m.UserName)
                </td>
            </tr>
            <tr>
                <td>
                    @Html.ValidationMessageFor(m => m.UserName)
                </td>
            </tr>
     
     
            <tr>
                <td>
                    @Html.LabelFor(m => m.Password)
                </td>
            </tr>
     
            <tr>
                <td>
                    @Html.PasswordFor(m => m.Password)
                </td>
            </tr>
     
            <tr>
                <td>
                    @Html.ValidationMessageFor(m => m.Password)
                </td>
            </tr>
     
            <tr>
                <td>
                    @Html.CheckBoxFor(m => m.RememberMe)
                    @Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
                </td>
            </tr>
     
     
                        
        </table>
                
     
            <input type="submit" value="Log in" />
                            
            </div>  
             
        }
    </section>
    If there is some other way I should be done this, I would be glad to hear about it.
    Greg
  2. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 14 Dec 2013 Link to this post

    Hello Greg,

    Probably some script-s are re-loaded multiple times and same handlers are re-attached multiple times. 

    Does the problem exist if you put the form outside the window widget? Demonstrate your case with a small runnable demo so we can further investigate.

    Kind Regards,
    Petur Subev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Greg Gum
    Greg Gum avatar
    18 posts
    Member since:
    Jun 2009

    Posted 14 Dec 2013 Link to this post

    This is now resolved.
    The issue was that I was referencing the "unobtrusive" scripts inside the partial view, instead of in the layout view.  So the references were getting added multiple times.

    Greg
Back to Top