Making namespace available across all MVC Razor views

If you are intending to use the same namespace across multiple views, just include it in the web.config of the Views folder. In the following example I wanted the MyProject.WebUI.Properties namespace to be available to all views. The removes the need to add the using statement for this namespace to every view. Please note, once you’ve added the namespace you’ll need to close and reopen any open view files for intellisence to recognize the namespace.

    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="MyProject.WebUI.Properties" />

WordPress – Replace default footer copyright info

By default WordPress adds copyright and theme acknowledgement details to the footer. This can be changed by updating the footer.php file.

1. Open the Editor from the Appearance menu.
2. Select the relevant theme and find footer.php under the templates section.
3. Scroll down and you should find a

section or something similar.
4. In this div you should see the footer text, update as necessary and click the Update File button.

Rename cookie

You can adjust it in your web.config file, the default is ASPXAUTH:

   <authentication mode="Forms">
      <forms name=".CRM-Cookie" loginUrl="LoginPage.aspx" />

This is useful when running multiple applications from the same server under the same domain. CheckBoxList validation

A custom validator can be used to validate a CheckBoxList control. However its crucial the correct attributes are passed. The most common mistake is using the ControlToValidate in CustomValidator. By using this you encounter the following:


Instead just use the OnServerValidate attribute to point to a validation method in the code behind. In this method create a reference to CheckBoxList control and run any validation required.

MVC Ajax Sys is undefined message

I spent an entire day trying to figure out why my Ajax.Begin form helper was failing to update the target div container. By chance I noticed the Sys is undefined message flash past in the Chrome console window. I’ve seen this before with Ajax enabled websites and it usually points to the project not being configured to use Ajax correctly. The jquery.unobtrusive-ajax package has been installed through NuGET correctly but it hadn’t set the configuration correctly.

Make sure the jquery.unobtrusive-ajax.js file is loaded after jquery and <add key=”UnobtrusiveJavaScriptEnabled” value=”true” /> in included in the web.confg <appSettings> section.

Visual Studio Creates an unwanted _ViewStart.cshtml

Many people have noticed Visual Studio sometimes creates an unwanted _ViewStart.cshtml in MVC projects. This happens when a Views is created manually from the Visual Studio right click menu rather than a controller.

The _ViewStart.cshtml file can cause all sorts of unwanted issues, especially when used in conjunction with third party systems such as Sitecore (can cause infinite looping when rendering a view).

To prevent this file being created make sure Use a layout page is unticked or if the view will be used as a partial view tick Create as a partial view.

MVC model binding with helpers

It is possible to bind your HTML controls to a model without the use of helpers such as @Html.TestboxFor()

Take this controller

public class MyControllerController : Controller
     public ActionResult Submit()
         return View(new MyViewModel());

     public ActionResult Submit(MyViewModel model)
            // model should be not null, with properties properly initialized from form values
            return View(model);

and model

public class MyNestedViewModel
    public string AnotherProperty { get; set; }

public class MyViewModel
    public MyViewModel()
         Nested = new MyNestedViewModel();

    public string SomeProperty { get; set; }

    public MyNestedViewModel Nested  { get; set; }

The values can be bound to the model using pure HTML.

<form method="POST" action="MyController/Submit">
    <div><label>Some property</label><input type="text" name="SomeProperty" /></div>
    <div><label>Another property</label><input type="text" name="Nested.AnotherProperty" /></div>
    <button type="submit">Submit</button>

C# Virtual Methods

A vertical method can be redefined in derived (class which inherit from this) classes. A virtual method has an implementation in both the base and derived class. These methods are used when the base class provides the required functionality most of the time, but may need changing by a derived class on occasion. A virtual method can be redefined by the derived class using the override keyword with the same method name. Methods which are not defined as virtual cannot be overriden in a derived class and must use the functionallity provided in the base class.

Example – Both the Rectangle and Circle classes inherit from the Shape class. However has the area of a circle is calculated differently to a rectangle, the circle class overrides to Area method. As the Colour method isn’t virtual both derived classes have to use the same method so in this case both will be red.

class Shape
	public string Colour()
		return "Red";
	public virtual double Area(double width, double height)
		return width*length;

class Rectangle : Shape
	public Rectangle()
		string colour = Colour();
		double width = Area(10.5,15);
class Circle : Shape
	public Circle()
		string colour = Colour();
		double width = Area(15);
	public override double Area(double radius)
		return 3.141*raduis*2;