UrlHelper extension method not working - asp.net-mvc-2

I'm trying to add an extension method to my MVC 2 project without success and after several hours of googling and looking here I'm at a loss. I've created a brand new MVC 2 project to make sure there was not anything weird about my existing project and I'm still facing the same problem. I'm sure this is a situation of I "can't see the forest for the trees" so any help would be greatly appreciated. Here is the code for the extension method.
using System.Web.Mvc;
namespace ExtensionTest.Helper
{
public static class UrlExtensions
{
public static string Image(this UrlHelper helper, string fileName)
{
return helper.Content("~/Content/Images/" + fileName);
}
}
}
and here is the code in the view (standard home index view created by default for a new MVC 2 project)
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%# Import Namespace="ExtensionTest.Helper" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= UrlHelper.Image("test") %>
<h2><%: ViewData["Message"] %></h2>
<p>
To learn more about ASP.NET MVC visit http://asp.net/mvc.
</p>
</asp:Content>
In design mode, when I type UrlHelper, intellisense does not show my extension method Image and if I run the project I get the following error:
CS0117: 'System.Web.Mvc.UrlHelper' does not contain a definition for 'Image'
At first I thought it was as simple as not adding a reference (Import statement), but that does not appear to be the case. The really weird thing to me is that I can add extension methods to the HtmlHelper object without issue in this same project.
Thanks in advance for any help provided.

Extension methods in .NET should be invoked on an object instance and not on the class itself (even though they are static).
So instead of:
<%= UrlHelper.Image("test") %>
try:
<%= Url.Image("test") %>

Related

Is it possible to incorporate ejs code into grapesjs?

I'm using grapesjs on a project and I want the user to be able to drag in a block that would add an ejs script, for example <%= data.title %>
I've tried doing it just like that, but the exported html is <%= data.title %<
plus it looks ugly in the editor.
Has anyone had a similar problem and made a solution?

Tab completion for ejs tags such as <% %>

I am using ejs for my template engine. In the .ejs files, it is fine to tab completion with normal html tags. However, I cannot find a way to tab completion for ejs tags such as <% %>``<%= %> etc. How could I make it?

Calendar extender not popping up

I have no idea why my ajax calender extender not popping up with I click on my textbox
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
Please select Start date<asp:TextBox ID="TextBoxstart" runat="server"></asp:TextBox>
<cc1:CalendarExtender ID="TextBoxstart_CalendarExtender" runat="server"
Format="dd/MM/yyyy" TargetControlID="TextBoxstart">
</cc1:CalendarExtender>
Please Select End Date <asp:TextBox ID="TextBoxend" runat="server"></asp:TextBox>
<cc1:CalendarExtender ID="TextBoxend_CalendarExtender" runat="server"
Enabled="True" TargetControlID="TextBoxend">
</cc1:CalendarExtender>
When I click in the textbox nothing happens. I have also included the ajaxcontroltoolkit.dll in the bin folder and nothing. I have other projects with this control and no problem at all. I have also used and still nothing
from your current code, TextBoxstart is not in the code snippet. Only "TextBoxend".
The end date should work as you have the calendarextender's TargetControlID set to it. Just not for TextBoxstart.

asp:Menu in master page pushes the content below on hover

I’m working on asp.net web site in .net 4.0 framework (VS 2010) in Win XP SP2.
In site.master page I have a control and it is populated dynamically. All is well in populating the control, but when mouse is hover on this menu item, it displays correct, but it pushes the content of page below (in my example, the content of “MainContent” is pushed below).
I tried changing the property of div, but no joy.. any help will be much appreciated.
Content of Site.Master
**<div class="title">
<h1>
My ASP.NET Application
</h1>
</div>
<div class="clear hideSkiplink">
<asp:Menu ID="mnuMain" runat="server" BackColor="#FFFFCC" CssClass="menu">
<DynamicItemTemplate>
<%# Eval("Text") %>
</DynamicItemTemplate>
</asp:Menu>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="footer">
</div>**
Sample code to add it dynamically
**MenuItem item = new MenuItem();
MenuItem item1 = new MenuItem();
item.Text = "Main Menu";
item1.Text = "sub menu1";
item.ChildItems.Add(item1);
mnuMain.Items.Add(item);**
You have got a clear on the div that wraps the menu. Instead of clear the wrapper should be floated.
Is there any CSS relate to the mark up above?

Limit JavaScript and CSS files on ASP.NET MVC 2 Master Page based on Model and View content

I want to include certain .js and .css files only on pages that need them.
For example, my EditorTemplate DateTime.ascx needs files anytimec.js and anytimec.css.
That template is applied whenever I use either the EditorFor or EditorForModel helper methods in a view for a model with a DateTime type value.
My technique:
I've put this condition into the <head> section of my master page. It checks for a DateTime type property in the ModelMetadata.
<% if (this.ViewData.ModelMetadata.Properties.Any(p => p.ModelType == typeof(DateTime))) { %>
<link href="../../Content/anytimec.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/anytimec.js" type="text/javascript"></script>
<% } %>
This has two problems:
Fails if I have nested child models of type DateTime
Unnecessarily triggered by views without EditorFor or EditorForModel methods (example: DisplayForModel)
How can I improve this technique?
Personally, I would use a partial view and an ASP Content Placeholder.
In the Views/Shared have a partial view EditorScripts.ascx which contains the tags defining the scripts to be included in your editing pages.
Then put a placeholder in the Site.Master <head> tag, something like this:
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
The, in any view that you want/need the scripts, put this code:
<asp:Content ContentPlaceHolderID="HeadContent" runat="server">
<% Html.RenderPartial("EditorScripts"); %>
</asp:Content>
This isn't a perfect solution and isn't as dynamic as you would like. The reason for using a partial view is that if you decide to update or add more scripts to those views, then you only have to update it in one location.
Another way would be to have a Editor.Master page that contains these scripts and other editor specific things then have that master use the Site.Master as it's master page. Then all editor views would have the Editor.Master as their master page.
HTH
I think the telerik web asset manager allows you to accomplish what you want and is open source.