AjaxHelper is not working - asp.net-mvc

I am working on ASP.NET MVC.
in order to use AjaxHelper, I inserted two javascript files in the site.master as following.
<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
then I used AjaxHelper in view pages, but it did not work.
so, I changed order of javascript file as following.
<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
Then it worked :(
So, my question is that the order of JavaScript file effects to use AjaxHelper class?

Absolutely. The MVCAjax classes will need to reference the more generic Ajax classes. Therefore, the generic ones get referenced first

The MicrosoftMvcAjax.debug.jss file has code in it that references code in MicrosoftAjax.debug.js. Hence the order in which they are declared matters.

Related

MVC 4 Portable Areas CSS/JS minification

I used MVC 3 + Contrib Project PortableAreas to split my web to multiple projects.
Now I moved to MVC 4 and want to use new feature minification for my css and JS.
But when I do:
<link href="#Url.Content("~/DSB/Styles/CSS")" rel="stylesheet" type="text/css" />
<script src="#Url.Content("~/Scripts/common/js")" type="text/javascript">
instead of:
<link href="#Url.Content("~/DSB/Styles/Site.css")" rel="stylesheet" type="text/css" />
<link href="#Url.Content("~/Content/themes/smoothness/jquery-ui-1.8.12.custom.css")" rel="stylesheet" type="text/css" />
<script src="#Url.Content("~/Scripts/common/jquery-1.4.4.min.js")" type="text/javascript" />
<script src="#Url.Content("~/Scripts/common/jquery-ui-1.8.11.custom.min.js")" type="text/javascript"></script>
it does not work.
How to use bundling/minification in Portable areas?
thanks
The problem here is that the bundling and minification does not foresee handling anything else than actual files. I have 2 solutions here that work.
Extract the files into temp files - requires write privileges for a folder within the app. Here is the code to handle that. It uses a directory called "Static" as the temporary target path. You can use it like this:
bundles.Add(new Rebundler(assemblyWithPortableArea, "~/VirtualPathUsedForResource")
.Include("Fully.Qualified.Embeded.Resource.Name", "other...")
.Rebundle());
Using it in a template is exaxctly as if you would use it in a non portable app, so #Scripts.Render() or #Styles.Render()
The other solution involves creating a bunch of classes that will allow you to use embeded resources. Here is the base class, and here are the script and style bundles. Here's the usage:
bundles.Add(new EmbededStyleBundle(assemblyWithPortableArea, "~/VirtualPathUsedForResource")
.Include("~/AreaName/Content/themes/custom/jquery-ui.css"));
With this approach, you need to use this class to render the resources. So instead of using the #Scripts.Render() or #Styles.Render() the template code looks like this:
#Assets.RenderStyles("virtual path here")
#Assets.RenderScripts("virtual path here")
Note that this code is far from clean. It has been mostly reverse engineered and may skip a few paths, but it seems to work so far.

CKEDITOR Problem on MVC2

I don't know how to use CKEditor in Mvc2 Framework.
I tried but it will not work
<%= Html.TextAreaFor(model => model.ABSTRACT) %>
<script type="text/javascript">
$(document).ready(function() {
$("$ABSTRACT").ckeditor();
});
</script>
Change the $ with # and it should definitely works
$("#ABSTRACT").ckeditor();
also be sure to have this 2 scripts referenced in your page.
<script type="text/javascript" src="#Content.Url("~/yourpath/ckeditor/ckeditor.js")"></script>
<script type="text/javascript" src="#Content.Url("~/yourpath/ckeditor/adapters/jquery.js")"></script>
You can also consider to associate ckEditor to a class instead of the Id of controls. so with only one call you can set the editor for all the control that have that class in your apps.
Is there any way you could just use the asp.net usercontrol for ckeditor?
http://ckeditor.com/download 3rd one down.
I have not tried it in mvc but I do not see why it would not work in MVC.

What is Reference Script Library in asp.net mvc 3?

I noticed when you make a strongly typed view you have an option to check a box called "reference script libraries"
http://gyazo.com/289392fc957866f19c4ae6d8b51a037e.png
In my case I don't have any strongly typed views but what references does it add?
It just adds this:
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

In ASP.NET MVC, adding a script reference throws exception

On a skeleton ASP.MVC that Visual Studio creates, I add a script tag to my head section in Site.Master:
<script src="~/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
This causes the page to not render. In my case, I had a custom controllerfactory and the base method GetControllerInstance threw an exception:
The controller for path
'/~/Scripts/jquery-1.3.2.js' could not
be found or it does not implement
IController.
Using "../../Scripts/jquery-1.3.2.js" for the src does not work either.
The only way it works is:
<script src="<%= Url.Content("~/Scripts/jquery-1.3.2.js") %>"
type="text/javascript"></script>
Then of course, the intellisense does not work for jquery. So I have to resort to adding the hack:
<% if (false) { %>
<script src="~/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<% } %>
which the hotfix was supposed to fix according to ScottGu
A line above is a link to a stylesheet:
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
For some reason, that works fine. Whether I use the virtual or relative path, I can see that the resulting url on the page is "Content/Site.css". The same can't be said for the jquery url. jquery link is returned as is - the jquery url is returned on the page containing the "~" or the "../..".
Can someone tell me what is going on?
Thanks
UPDATE:
Thanks to the commenters, I remembered that ~ is an asp.net thing. My only question is then why doesn't the same issue exist for the stylesheet? The link tag above, for example, I can put ~ or relative paths and it always comes out right. Where is the magic?
Have you tried without the ~:
<script src="/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
The ~ character is used only by server side processing scripts indicating the root of the web site. The reason:
<script src="<%= Url.Content("~/Scripts/jquery-1.3.2.js") %>" type="text/javascript"></script>
works is because it is translated to:
<script src="/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
The "~" is not supported within standard HTML - it is an ASP.NET shortcut. So you've either got to do it the way you specified in your OP and the hack for intellisense, or as Darin specified but then you lose the ability to automatically pick up your VRoot.
I don't know for sure, but maybe MVC is smart enough to not include CSS files in the routing.

CSS and Javascript relative path confusion in ASP.NET MVC

My javascript paths work on this page:
http://localhost:53049/
But not on this page:
http://localhost:53049/Home/Messages
The reason is that the relative paths are different, the former requires ("js/...") and the latter requires ("../../js/...").
I'm including my javascript in my Site.Master file:
<script type="text/javascript" src="js/jquery.jqGrid.js"></script>
<script type="text/javascript" src="~/js/jquery.jqGrid.js"></script>
<script type="text/javascript" src="<%= this.ResolveClientUrl("~/Scripts/jquery-1.2.6.js") %>"></script>
How do I get around this relative path madness, i.e. what is the best practice way in ASP.NET MVC to set CSS/Javascript paths in the Site.Master so that they work for each view no matter how deep that view's URL happens to be.
ADDENDUM:
It seems that for the Index view, any path will work, strangely:
<script type="text/javascript" src="/Scripts/jquery-1.2.6.js"></script>
<script type="text/javascript" src="../../Scripts/jquery-1.2.6.js"></script>
<script type="text/javascript" src="../../../Scripts/jquery-1.2.6.js"></script>
But for any other pages (pages with a deeper URL), none of these work.
What's going on here? How can we set the Javascript path once in Site.Master and they work for all pages?
ADDENUM II:
It turned out to only be a problem with the jqgrid javascript file (not the jquery file), apparently inside that file it is referencing other javascript files and gets confused:
<script type="text/javascript" src="<%= Url.Content ("~/js/jquery.jqGrid.js") %>"></script>
You can also use the Url.Content method call to make sure that the paths are correctly set.
Examples can be found here.
Regarding paths used in CSS documents:
/Content/site.css
Body {background-image:url('background.jpg');}
Relative paths in CSS documents are related to the CSS document, not the document being displayed.
This information saved me some headaches.
Try setting the javascript to use a forward slash at the beginning, like "/js/jquery.jqGrid.js" This will set them to use the root domain instead of relative pathing.
The solution for jqGrid: open the file jquery.jqGrid.js and locate the line:
var pathtojsfiles = "js/"; // need to be ajusted
As the comment says, you need to edit that path, e.g. for a typical ASP.NET MVC app,
var pathtojsfiles = "/Scripts/js/"; // need to be ajusted
You need to add runat="server" and to specify the absolute path like this:
<script type="text/javascript" runat="server" src="~/js/jquery.jqGrid.js"></script>]

Resources