开发者

ASP.NET MVC jQuery script added in View not working

开发者 https://www.devze.com 2022-12-19 10:30 出处:网络
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 direc

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 cat开发者_运维技巧egory)
{
  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


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消