Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery or Javascript problem in my partial view

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

like image 474
user300485 Avatar asked May 04 '11 18:05

user300485


People also ask

Can we use jQuery in partial view?

Then, in the view page, after the document is ready, you can use JQuery to get the Selected value, if the selected value is not the first option ("0"), load the partial view to display the related content.

Can I use JavaScript in partial view?

JavaScript functions can be bound to elements on the partial view; the partial view is rendered at the same time as the parent view. This happens when loading the partial view with a @Html. Action helper method, as in this section from Edit.

Can I use both JavaScript and jQuery together?

jQuery is a JavaScript library, so it operates on top of JavaScript. It cannot exist on its own, so you can't use one over the other. You can use just JavaScript or JavaScript and jQuery. jQuery was introduced to make development with JavaScript easier.

How do you check if a view is partial or not?

Solution 1 There is no difference in the markup between a partial view and a view; the only difference is in how they are delivered to the browser. A partial view is sent via AJAX and must be consumed by JavaScript on the client side, while a View is a full postback. That's it.


3 Answers

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

like image 74
Priyank Avatar answered Oct 25 '22 19:10

Priyank


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

like image 37
ariel Avatar answered Oct 25 '22 17:10

ariel


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>
like image 34
Gary Green Avatar answered Oct 25 '22 18:10

Gary Green