How to enable custom html helper extension method to all views? - asp.net-mvc

I have an issue on custom html helper method. I have added two custom html helper method. First method is MyTextBox and second one is MySelectBox Method which is extention to the HtmlHelper class.The following code is working fine.
C# :
namespace MyHtmlHelper
{
public static class HtmlHelperClass
{
public static MvcHtmlString MyTextBox(string fieldName)
{
return new MvcHtmlString("<input type=\"text\" name=\"" + fieldName + "\"></input>");
}
}
}
namespace MyHtmlHelper
{
public static class HtmlHelperExtension
{
public static MvcHtmlString MySelectBox(this HtmlHelper helper, string text)
{
return new MvcHtmlString("<select ><option>" + text + "</option></select>");
}
}
}
view:
#using MyHtmlHelper
<div>
#MyHtmlHelper.HtmlHelperClass.MyTextBox("test")
#Html.MySelectBox("Test")
</div>
I want this html helper method for all views in my application.so i removed MyHtmlHelper namespace from view and added in web.cong as follows
Web.config :
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
<add namespace="MyHtmlHelper"/>
</namespaces>
</pages>
My Problem is : MyTextBox helper method working fine but html extension method (MySelectBox) is not working.Anyone please help me ,what mistake i have done or how to resolve this issue?

There is a another webconfig in the view folder. You have to use this.

Related

My namespace is not found in MVC 5 view

Im trying to create my own helpers but im totally stuck with this error. I have searched the web and read soooo many posts about this kind of issue.
When I try to add the namespace to the view I get the error "The type or namespace 'CustomHelpers' could not be found..."
So I created a class for my helper and added the namespace to the Views web.config.
My helper class
using System.Web.Mvc;
namespace CustomHelpers
{
public static class CustomHelpers
{
public static string Truncate(this HtmlHelper helper, string input, int length)
{
if (input.Length <= length)
{
return input;
}
else
{
return input.Substring(0, length) + "...";
}
}
}
}
My web.config
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="Playground.Web" />
<add namespace="CustomHelpers"/>
</namespaces>
</pages>
</system.web.webPages.razor>
My View
#using CustomHelpers
#{
ViewBag.Title = "Home Page";
}
The problem is you should not name a class the same as its namespace.
Do not name a class the same as its namespace
For example,
namespace YourProjectName.Framework
{
public static class CustomHelpers
{
...
}
}

ASP.NET MVC: Referencing a model

I am setting up a new ASP.NET MVC 5 project in Visual Studio. My project is called MyProject. I have a model called MyModel, and a controller called MyController:
\\ MyModel.cs
namespace MyProject.Models
{
public class MyModel
{
public int Width { get; set; }
public int Height { get; set; }
}
}
\\ MyController.cs
using MyProject.Models;
namespace MyProject.Controllers
{
public class MyController : Controller
{
public ActionResult Index()
{
MyModel my_model;
return View(my_model);
}
}
}
However, in MyController.cs, I get the error message: The type or namespace 'MyModel' could not be found. Why is this? I have already included using MyProject.Models;, so shouldn't this be sufficient?
Thanks!
using MyProject.Models; should indeed be sufficient. Have you tried Rebuilding the solution (not build!) and restarting visual studio? My VS2013 needs a restart from time to time as well if I have been messing around with too many references.
Are you possibly receiving the error within your view? Try adding the namespace to the appropriate section within the web.config file found under your views folder.
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="MyProject.Models" />
</namespaces>
</pages>
</system.web.webPages.razor>

How to create own WebViewPage in mvc?

I want to create own WebViewPage class so that i have created my class which is shown below
public abstract class iBlueWebViewPage<TModel>:WebViewPage
{
protected iBlueWebViewPage()
{
}
public HtmlHelper<TModel> iBlue { get; set; }
}
for example if we want text box in mvc we can code #html.TextBox("Name")
like that i need #iBlue.TextBox("Name") instead of #html
How can i achieve that ?
Thanks in advance..
You should change base type of pages, from web configuration (pages element)
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory,
System.Web.Mvc, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="MyNamespace.iBlueWebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
More detailed reading at Phil Haack's article (example is taken from there)

Problem with HtmlHelper and MVC not seeing my new method

I tried to create this HtmlHelper method:
namespace Power.WebUx.Helpers
{
public static class HtmlHelperExtensions
{
public static MvcHtmlString SelectedIfMatch(this HtmlHelper helper, string actual, string expected)
{
if (expected == actual)
{
return new MvcHtmlString("<option selected=\"selected\" value=\"" + actual + "\"" + actual + "</option>");
}
else
{
return new MvcHtmlString("<option value=\"" + actual + "\"" + actual + "</option>");
}
}
I added the Power.WebUx.Helpers line to my web.config:
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
<add namespace="Power.WebUx.Helpers" />
</namespaces>
</pages>
However when I try to use the extension I get an error message saying that System.Web.Mvc.HtmlHelper does not contain a definition for SelectedIfMatch
Does the code I am trying to run look right or am I missing something?
Hope someone can see something obvious.
thanks
Jon Wylie
Import the namespace into your view to use any extension methods in that namespace
<%# Import Namespace =
"Power.WebUx.Helpers" %>
Make sure you're modifying the top level web.config file(instead of the one in the views folder), then close and open the files where you're trying to use the helper

Generate urls that use MapRoute defaults

I have these routes defined:
routes.MapRoute("CategoryList_CountryLanguage", "Categories/{id}/{urlCategoryName}/{country}/{language}",
new {
controller = "Categories",
action = "Details",
});
routes.MapRoute("CategoryList", "Categories/{id}/{urlCategoryName}",
new {
controller = "Categories",
action = "Details",
country = "US",
language = "EN"
});
and I'm generating links using:
#Html.ActionLink("desc", "Details", "Categories", new { id = item.Id, urlCategoryName = item.UrlFriendlyName}, null)
and the generated urls are in the form:
/Categories/id/friendly-name
I want to generate:
/Categories/id/friendly-name/US/EN
without having to specify the country and language in the ActionLink call, can't I use defaults like that?
The easy workaround is to specify those parameters in the ActionLink calls, but I would like to avoid that if possible. My hope is that the first route expects the values specified in the url, while the second has the defaults when not included in the url and would use that to create new urls, no luck so far, is this possible?
You can create a helper class called UrlHelpers.cs that looks like this:
public static class URLHelpers {
public static string CategoryList(this UrlHelper helper, int id, string urlFirendlyName) {
return CategoryList(helper, id, urlFirendlyName, "US", "EN");
}
public static string CategoryList(this UrlHelper helper, int id, string urlFirendlyName, string country, string language)
{
return helper.Action("Details", "Categories", new { id, urlCategoryName = urlFriendlyName, country, language });
}
}
Then in your view you would call it like this:
Some Text
Just a note: You will want to add the namespace of your helper to your web.config in the pages > namespaces section. So if you add a Helpers folder to the root of your MVC app and place the UrlHelper.cs class in it you would add:
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
<add namespace="MyProject.Helpers"/>
</namespaces>
</pages>

Resources