Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you bind a nested viewmodel list with a custom modelbinder?
    text
    copied!<p>I'm trying to bind a viewmodel with a dynamic list of nested viewmodels. I figured out on how to bind 1 viewmodel that is nested. But how do I bind a list of them?</p> <p>My classes:</p> <pre><code>public class PracticeViewModel { public List&lt;OpeningHourViewModel&gt; OpeningHours { get; set; } } public class OpeningHourViewModel { public OpeningHourViewModel() { Id = Guid.NewGuid(); From1 = DateTime.Today; From2 = DateTime.Today; From3 = DateTime.Today; To1 = DateTime.Today; To2 = DateTime.Today; To3 = DateTime.Today; } [HiddenInput(DisplayValue = false)] [Browsable(false)] public Guid Id { get; set; } public DayOfWeek DayOfWeek { get; set; } [Display(Name = "From", ResourceType = typeof(Resources))] public DateTime From1 { get; set; } [DateGreaterThanAttribute("From1")] [Display(Name = "To", ResourceType = typeof(Resources))] public DateTime To1 { get; set; } [Display(Name = "From", ResourceType = typeof(Resources))] public DateTime From2 { get; set; } [Display(Name = "To", ResourceType = typeof(Resources))] [DateGreaterThanAttribute("From2")] public DateTime To2 { get; set; } [Display(Name = "From", ResourceType = typeof(Resources))] public DateTime From3 { get; set; } [Display(Name = "To", ResourceType = typeof(Resources))] [DateGreaterThanAttribute("From3")] public DateTime To3 { get; set; } public HourSchedule HourSchedule { get; set; } public bool HasMorningOpening { get { return From1.TimeOfDay != new TimeSpan() &amp;&amp; To1.TimeOfDay != new TimeSpan(); } } public bool HasAfternoonOpening { get { return From2.TimeOfDay != new TimeSpan() &amp;&amp; To2.TimeOfDay != new TimeSpan(); } } public bool HasEveningOpening { get { return From3.TimeOfDay != new TimeSpan() &amp;&amp; To3.TimeOfDay != new TimeSpan(); } } </code></pre> <p>My modelbinder (so far):</p> <pre><code> public class PractieModelBinder : IModelBinder { private readonly IPracticeService _practiceService; public PracticeModelBinder(IPracticeService practiceService) { _practiceService = practiceService; } public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var id = (Guid)controllerContext.RouteData.Values["Id"]; var viewModel = Map.This(_practiceService.GetPractice(id)).To&lt;PracticeViewModel&gt;(); return viewModel; } private T TryGet&lt;T&gt;(ModelBindingContext bindingContext, string key) where T : class { if (String.IsNullOrEmpty(key)) return null; ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "." + key); if (valueResult == null &amp;&amp; bindingContext.FallbackToEmptyPrefix) valueResult = bindingContext.ValueProvider.GetValue(key); bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueResult); if (valueResult == null) return null; try { return (Nullable&lt;T&gt;)valueResult.ConvertTo(typeof(T)); } catch (Exception ex) { bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex); return null; } } } </code></pre> <p>Thanks in advance,</p> <p>EDIT:</p> <p>It takes 10 seconds to validate my model with the default model binder and client side validation on. My model is alot more complex than this, but ive hided most of it. I've read that using a custom model binder can improve response time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload