My namespace is not found in MVC 5 view - asp.net-mvc

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
{
...
}
}

Related

How to enable custom html helper extension method to all views?

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.

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)

Register generic page base class

I create my own generic page base class:
public abstract class ViewPageBase<TModel> : WebViewPage<TModel>
{
public ParamBuilder<TModel> Param { get { return new ParamBuilder<TModel>(Model); } }
}
Web.config:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="ViewPageBase">
<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>
This entry causes the following errors:
INCORRECT_TYPE_PARAMETER_NUMBER
And
An expression tree may not contain a dynamic operation
How do I register a generic page base class with ASP.NET MVC4 without using the #inherits keyword in each Razor view?
This should work. The error you are getting is not related to the custom view page.

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

Resources