How to fill ViewModel which has List<T> property after clicking submit button mvc 3?


  • Hi; i want to make day of week checkbox from viewModel.Every thing is ok on View page. But if i click submit button to save changes look please httppost method weeklySchedule is null. Why weeklySchedule is null.. How to see selected checkbox vlue on my httpost method model instance? How to fill Settings? Or do you have any advise thansk alot for your helps.


     public class Settings
            public class WeeklySetting
                public string DisplayName { get; set; }
                public string Value { get; set; }
                public bool IsChecked { get; set; }

    View Model:

    public class ViewModels
            public class Weekly
                public List<Settings.WeeklySetting> Settings { get; set; }
       public ActionResult CreateWeekly()
               var model = new ViewModels.Weekly
                     Settings = new[] 
                     new Settings.WeeklySetting{ DisplayName="Monday", Value="MON", IsChecked=false},
                     new Settings.WeeklySetting{ DisplayName="Tuesday", Value="TUE", IsChecked=false},
                     new Settings.WeeklySetting{ DisplayName="Wednesday", Value="WED", IsChecked=false},
                     new Settings.WeeklySetting{ DisplayName="Thursday", Value="THU", IsChecked=false},
                     new Settings.WeeklySetting{ DisplayName="Friday", Value="FRI", IsChecked=false},
                     new Settings.WeeklySetting{ DisplayName="Saturday", Value="SAT", IsChecked=false},
                      new Settings.WeeklySetting{ DisplayName="Sunday", Value="SUN", IsChecked=false},
                   return View(model);
           public ActionResult CreateWeekly(string submitButton, ViewModels.Weekly weeklySchedule)
    		//Do something
               return View("CreateWeekly", weeklySchedule);


    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyProject.Controllers.ViewModels.Weekly>" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm())
          { %>
          <%foreach (var item in Model.Settings)
                <%:Html.CheckBoxFor(q=>item.IsChecked) %>
                <%:Html.LabelFor(q=>item.IsChecked,item.DisplayName) %>
                <%:Html.HiddenFor(q=>item.Value) %>
           <% } %>
        <br />
           <input value="GenerateForWeekly" name="submitButton" type="submit" />
              <%} %>

    viernes, 06 de julio de 2012 11:45