Binding a TimeSpan Model from the Whole

I would like to declare some properties of my TimeSpan view model to display the TotalMinutes property and bind it to TimeSpan .

I bound the property without using a strongly typed helper to get the TotalMinutes property:

 <%=Html.TextBox("Interval", Model.Interval.TotalMinutes)%> 

When the field is attached to the View Model class, it analyzes the number as a day (1440 minutes).

How can I override this behavior for certain properties (preferably using the attributes of the view model itself)?

+6
c # asp.net-mvc
source share
1 answer

Writing a custom binder looks like a good idea here:

 public class TimeSpanModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".TotalMinutes"); int totalMinutes; if (value != null && int.TryParse(value.AttemptedValue, out totalMinutes)) { return TimeSpan.FromMinutes(totalMinutes); } return base.BindModel(controllerContext, bindingContext); } } 

And registering it in Application_Start :

 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); ModelBinders.Binders.Add(typeof(TimeSpan), new TimeSpanModelBinder()); } 

And finally, always prefer strongly typed helpers in your opinion:

 <% using (Html.BeginForm()) { %> <%= Html.EditorFor(x => x.Interval) %> <input type="submit" value="OK" /> <% } %> 

And the corresponding editor template ( ~/Views/Home/EditorTemplates/TimeSpan.ascx ):

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TimeSpan>" %> <%= Html.EditorFor(x => x.TotalMinutes) %> 

Now your controller can be as simple as:

 public class HomeController : Controller { public ActionResult Index() { var model = new MyViewModel { Interval = TimeSpan.FromDays(1) }; return View(model); } [HttpPost] public ActionResult Index(MyViewModel model) { // The model will be properly bound here return View(model); } } 
+10
source share

All Articles