views:

3919

answers:

4

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:

http://channel9.msdn.com/posts/Glucose/Hanselminutes-on-9-ASPNET-MVC-2-Preview-1-with-Phil-Haack-and-Virtual-Scott/

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

--Edit--

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">
<p>
    <% Html.EditorFor(c => c.message); %>
</p>
<p>
    <% Html.EditorFor(c => c.number); %>
</p>
<p>
    <% Html.EditorFor(c => c.Date); %>
</p>

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.
calebt
+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="StartDate.abc" 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:

/Views/Shared/EditorTemplates/DateTime.ascx:

<%@ 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.

/Views/Shared/EditorTemplates/DateTime.ascx:

<%@ 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' });
</script>

-Matt

Matt Frear
+1  A: 

I've written a blog post about how to do this by creating reusable templates in MVC 2 http://bit.ly/a48frG

My post also explains the relationship between TemplateInfo and templates.

DalSoft
A: 

wrong thread... just ignore this

Denis