1

I have an Index.cshtml file. In that I have two div elements. I rendered partial view in one div like this:

<div id="div1">
    @Html.Partial("_FutureAddEditClaim", Model.NewClaim)
</div>
<div id="div2"></div>

And in javascript, I created two new div elements. In one of these I appended div1. And in _FutureAddEditClaim.cshtml I have a function which runs on load:

$(function() {
    alert("called");
});

When the application runs the alert message is showing twice. Can any one please tell me why this is happening in _FutureAddEditClaim.cshtml.

I had the page load function in Index.cshtml. This works fine.

2
  • All you need to know is the final output: Does it contain one or two alerts? (view-source of the page in browser) Now you know if the problem is with the server side code or client side code. Commented Nov 29, 2011 at 13:31
  • I had seen source it contains only one alert message in page load.. Commented Nov 29, 2011 at 13:36

2 Answers 2

1

You said that you "appended div1" in one of the div elements. That seems to be the reason the JavaScript would be firing twice since you have the element loading twice -- once initially and then again after appending it.

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

2 Comments

Thank you.. But i need to append the element... How can i resolve the issue.. How can i get only one page load..
Off the top of my head (and this may not be the best option), you could try setting a variable (call it isLoaded) when the page load event is fired the first time. Then have a check in the code to see if isLoaded has been set already so the code doesn't fire twice.
0

This does not denote an event on page load. This will run when the document is ready and you can have many blocks that run when the document is ready.

$(function() {
    // Runs when the document is ready
    alert("called");
});

You need to scan the source file to see if either...

  1. There are two similar blocks of code in your source file that run when the document is ready
  2. There is another alert("called") somewhere in your source file
  3. If you load the partial view via AJAX, for example, it may happen each time you load it

1 Comment

Thank You... But I had no two alerts and no similar code and i am not loading the partial view via Ajax..

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.