I want to create a MVC 2 editor template for a value type i.e. int , has anyone done this with the preview 1 bits?

Many thanks

+3  A: 

I have not tried preview 1 yet but they did what you are asking for in this channel9 video:

They do both DisplayFor and EditorFor, starts around 2 minutes.


For value type i.e. int I was able to get it to work in the same way.

Create a model to pass to my view:

public class HomeController : Controller
    public ActionResult Index()
        HomeModel model = new HomeModel();
        model.message = "Welcome to ASP.NET MVC!";
        model.number = 526562262;
        model.Date = DateTime.Now;

        return View(model);

public class HomeModel
    public string message { get; set; }

    public int number { get; set; }

    public DateTime Date { get; set; }

Link view to the model using the new template logic:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<HomeModel>" %>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <% Html.EditorFor(c => c.message); %>
    <% Html.EditorFor(c => c.number); %>
    <% Html.EditorFor(c => c.Date); %>

Then create a template for each of the types e.g. Int32:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
Editor For My Int32: <%= Html.TextBox("abc", Model.ToString())%>

I put this in Views\Shared\EditorTemplates\Int32.ascx

Nick Clarke
The video shows a creation of a editor template based on a reference type (string) not a value type.
dean: I improved my answer, but I just noticed in the release notes there is a known issue with value types. It seemed to work for me above so not sure what that is related to.
Nick Clarke
Thanks Nick, I was declaring the value type in my Page declaration, e.g. Inherits="System.Web.Mvc.ViewUserControl<int>" removing this solves the issue as per your example. Thanks for all your help
I think <T> has to be a reference type, so you can't do that for value types such as DateTime or int.
+8  A: 

Will Nick Clarke's answer work when you submit the values on postback?

In MVC2 preview 2, calling Html.Textbox("abc", Model.ToString()) will render a textbox with ".abc" appended to the name, e.g.

<input id="StartDate_abc" name="" type="text" value="02 Feb 09" />

which will cause problems when you postback and attempt to UpdateModel().

I did an editor template for a DateTime, the following works for me:


<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%= Html.TextBox(String.Empty, Model.ToString("dd MMM yy")) %>

or, to use jQuery's DatePicker for all your DateTimes add a reference to jQuery and jQueryUI to either your Masterpage or to the View containing the call to EditorFor.


<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%= Html.TextBox("", Model.ToString("dd MMM yy")) %>
<script type="text/javascript">
    $("#<%= ViewData.ModelMetadata.PropertyName %>").datepicker({ dateFormat: 'dd M y' });


Matt Frear
+1  A: 

I've written a blog post about how to do this by creating reusable templates in MVC 2

My post also explains the relationship between TemplateInfo and templates.


wrong thread... just ignore this