0

ビューモデルをネストされたビューモデルの動的リストにバインドしようとしています。ネストされた1つのビューモデルをバインドする方法を見つけました。しかし、それらのリストをバインドするにはどうすればよいでしょうか?

私のクラス:

public class PracticeViewModel
{
    public List<OpeningHourViewModel> 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() &&
            To1.TimeOfDay != new TimeSpan();

        }
    }

    public bool HasAfternoonOpening
    {
        get
        {
            return From2.TimeOfDay != new TimeSpan() &&
            To2.TimeOfDay != new TimeSpan();

        }
    }

    public bool HasEveningOpening
    {
        get
        {
            return From3.TimeOfDay != new TimeSpan() &&
            To3.TimeOfDay != new TimeSpan();

        }
    }

私のモデルバインダー(これまでのところ):

  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<PracticeViewModel>();



        return viewModel;
    }
    private T TryGet<T>(ModelBindingContext bindingContext, string key) where T : class
    {
        if (String.IsNullOrEmpty(key))
            return null;

        ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "." + key);
        if (valueResult == null && bindingContext.FallbackToEmptyPrefix)
            valueResult = bindingContext.ValueProvider.GetValue(key);

        bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueResult);

        if (valueResult == null)
            return null;

        try
        {
            return (Nullable<T>)valueResult.ConvertTo(typeof(T));
        }
        catch (Exception ex)
        {
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex);
            return null;
        }
    }
}

前もって感謝します、

編集:

デフォルトのモデル バインダーとクライアント側の検証をオンにして、モデルを検証するのに 10 秒かかります。私のモデルはこれよりもかなり複雑ですが、そのほとんどを隠しました。カスタム モデル バインダーを使用すると、応答時間が改善される可能性があることを読みました。

4

0 に答える 0