views:

81

answers:

3

Hello,

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 :

<%= Model.GetMyScript() %>

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

A: 

can you look at the rendered source, it may be the tags are not rendering correctly or maybe some of the special characters?

Pharabus
This should really be a comment, it's not an answer.
Lazarus
A: 

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.

Gregoire
+1  A: 

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>
Paddy