ASP.NET MVC 2 editor template for value types, int

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

Many thanks

+6
asp.net-mvc asp.net-mvc-2
source share
4 answers

I have not tried viewing 1 yet, but they did what you ask in this channel9:

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

They run both DisplayFor and EditorFor, starting in about 2 minutes.

- Edit -

For the type of value ie int, I managed to get it to work the same way.

Create a model to go 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 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 type, for example. 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

+4
source share

Will Nick Clark respond to work when you send values ​​via postback?

In MVC2 preview 2, calling Html.Textbox ("abc", Model.ToString ()) will display a text box with ".abc" appended to the name, for example.

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

which will cause problems with postback and trying UpdateModel ().

I made an editor template for 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 DatePicker for all your DateTimes, add a link to jQuery and jQueryUI either to the main page or to the view containing the EditorFor call.

/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> 

Update: ASP.NET MVC3 using Razor syntax:

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

And use all you need in your view is:

 @Html.EditorFor(model => model.DueDate) 

Matt

+15
source share

I wrote a blog post on how to do this by creating reusable templates in MVC 2.

My post also explains the relationship between TemplateInfo and templates.

+2
source share

I found the Brad Wilson blog to have better examples and explanations. Part-3 in the series deals specifically with value types (String, decimal, Int32).

Enjoy it!

+1
source share

All Articles