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
-
2You'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.Pointy– Pointy2010年02月09日 13:45:30 +00:00Commented Feb 9, 2010 at 13:45
-
1Do you add your partial view asynchronously (with ajax) to your page?Branislav Abadjimarinov– Branislav Abadjimarinov2010年02月09日 13:45:59 +00:00Commented 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.Paddy– Paddy2010年02月09日 13:49:35 +00:00Commented Feb 9, 2010 at 13:49
-
@Branislav Abadjimarinov not yet ... but I have to add that yes.TheBoubou– TheBoubou2010年02月09日 13:54:11 +00:00Commented 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 runningTheBoubou– TheBoubou2010年02月09日 13:55:11 +00:00Commented Feb 9, 2010 at 13:55
2 Answers 2
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>
Comments
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.