This is a migrated thread and some comments may be shown as answers.

Why is Ajax being called multiple times.

3 Answers 329 Views
Window
This is a migrated thread and some comments may be shown as answers.
Greg Gum
Top achievements
Rank 1
Greg Gum asked on 13 Dec 2013, 01:55 AM
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

3 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 14 Dec 2013, 11:49 AM
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!
0
Greg Gum
Top achievements
Rank 1
answered on 14 Dec 2013, 03:29 PM
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
0
omar
Top achievements
Rank 1
answered on 28 Aug 2020, 07:46 PM
Thank you , it working great, this is the simplest and shortest solution for prevent recalling the function 
Tags
Window
Asked by
Greg Gum
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Greg Gum
Top achievements
Rank 1
omar
Top achievements
Rank 1
Share this question
or