7

Can I use jQuery or JavaScript code in partial views?

I have a grid in my partial view and I am trying to hide one grid element using jQuery in that partial view. I am not able to do so. But same code works if I use it without a partial view.

Can anybody help me out?

Here is my code

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<NascoBenefitBuilder.ViewModels.Obn.ProductTemplate.ObnProductTemplateMainData>" %>
<script type = "text/javascript" language="javascript">
$(document).ready(function(){
    alert("success");
});
</script>

This code is in my partil view but when this page loads I am not able to popup this alert box.

thanks Thanks

3
  • 1
    try removing language="javascript" and if that doesn't work trying taking the alert() outside the $(document).ready event. Commented May 4, 2011 at 18:46
  • 1
    From the looks of it you're not referencing jQuery from within your Partial View Commented May 4, 2011 at 18:52
  • 1
    How is this partial included? Commented May 4, 2011 at 18:58

3 Answers 3

11

There could be few reasons for this not working.

Just to make sure your JQuery is available to partial view, you can try adding jquery references in your partial view page. Hoping you don't have similar issue like this one http://forums.asp.net/t/1649526.aspx/1

Second, if you are calling it through AJAX, javascript included in that partial view is not run by MVC AJAX. Follow this guideline to have it work for you.

http://geekswithblogs.net/DougLampe/archive/2010/11/12/execute-javascript-in-mvc-partial-view.aspx

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

Comments

2

Check the output page for nested tags, or if any markup is being escaped out. Also, check the browser's javascript console for errors.

Comments

2

It might be a conflict or the character $ being interpreted incorrectly, try doing:

<script type="text/javascript">
jQuery(document).ready(function(){
    alert("DOM loaded!");
});
alert("this script tag is executing properly!");
</script>

Comments

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.