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

    Pregunta

  • 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.

    Model:

     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},
                    }.ToList()
               };
                   return View(model);
               
           }
           [HttpPost]
           public ActionResult CreateWeekly(string submitButton, ViewModels.Weekly weeklySchedule)
           {
             
    		//Do something
               return View("CreateWeekly", weeklySchedule);
           } 
    

    View:

    <%@ 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">
        CreateWeekly
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
    <% using (Html.BeginForm())
          { %>
    
          <div>
          <%foreach (var item in Model.Settings)
            {%>
                
                <%:Html.CheckBoxFor(q=>item.IsChecked) %>
                <%:Html.LabelFor(q=>item.IsChecked,item.DisplayName) %>
                <%:Html.HiddenFor(q=>item.Value) %>
           <% } %>
          
          </div>
        <br />
           <input value="GenerateForWeekly" name="submitButton" type="submit" />
              <%} %>
    </asp:Content>




    viernes, 06 de julio de 2012 11:45