0

In my view, I generate the HTML, and add some jQuery/Javascript code. The view is displayed in a div of my main page. The jQuery code I added is there but not working. When I place the same code directly hard-coded in the view that's work. In my view, it's as partial view (.ascx) I have something like this :

Thanks,

Update 1 In my main page I have a menu section with hyperlink. In this partial view (not the same than the other), I create some link :

<h2>Category</h2>
<ul>
<% foreach (Category item in Model.Categories) { %>
<li><%= Model.DisplayURL(item) %></li>
<% } %>
</ul>

DisplayURL

public string DisplayURL(Category category)
{
 return string.Format("<a href=\"#\" onclick=\"MyFunction('{1}')\">{0}</a>", category.Code, category.Code);
}

When I add this at the end of partial view :

<script type='text/javascript' language='javascript'>
 function MyFunction(data){
 alert(data);
 }); 
 </script>

but the same code added in the view by programming is not running.

Error I receive : http://tinypic.com/r/55ms1l/6

asked Feb 9, 2010 at 13:37
6
  • 2
    You'd get help from the non-psychic Stackoverflow crowd if you'd post some of the relevant Javascript and perhaps describe what it does, or what it's supposed to do, and the incorrect behavior you see, and what you've tried so far to debug the problem. Commented Feb 9, 2010 at 13:45
  • 1
    Do you add your partial view asynchronously (with ajax) to your page? Commented Feb 9, 2010 at 13:45
  • How are you referencing your script files - you may need to use the ResolveUrl helper method to get the correct virtual reference. Commented Feb 9, 2010 at 13:49
  • @Branislav Abadjimarinov not yet ... but I have to add that yes. Commented Feb 9, 2010 at 13:54
  • @Paddy it's not a source file, the JS code is added by programming in the view but this code added is there but not running Commented Feb 9, 2010 at 13:55

2 Answers 2

1

I think that your function declaration is not syntactically correct, it's got an extra closing bracket and semi-colon at the end, it should just be:

 <script type='text/javascript' language='javascript'>
 function MyFunction(data){
 alert(data);
 }
 </script>
answered Feb 10, 2010 at 10:05
Sign up to request clarification or add additional context in comments.

Comments

0

If your partial view is rendered by an ajax Action, the script in this view will not be evaluated.

You should put your function in external script file.

answered Feb 9, 2010 at 14:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.