0

我想使用 viewModel 制作一周中的某一天复选框。视图上一切正常,但是,如果我单击提交以保存更改,weeklySchedule则在我的 httppost 方法中为空。

为什么为weeklySchedule空?如何将选定的复选框值发送到我的HttPost操作?任何建议都会有所帮助。

在此处输入图像描述

模型:

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

查看型号:

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);
   } 

看法:

<%@ 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()) { %>

  &#60;div&#62;
  &#60;%foreach (var item in Model.Settings)
    {%&#62;

        &#60;%:Html.CheckBoxFor(q=&#62;item.IsChecked) %&#62;
        &#60;%:Html.LabelFor(q=&#62;item.IsChecked,item.DisplayName) %&#62;
        &#60;%:Html.HiddenFor(q=&#62;item.Value) %&#62;
   &#60;% } %&#62;

  &#60;/div&#62;
&#60;br /&#62;
   &#60;input value="GenerateForWeekly" name="submitButton" type="submit" /&#62;
      &#60;%} %&#62;

</asp:Content>

4

2 回答 2

2

当您开始在视图中使用 foreach() { } 循环时,模型绑定不会很好地工作。如果您将 EditorTemplate 用于 WeeklySetting,您会发现它会更好地为您整理所有索引。

例如(这是 MVC3 Razor 视图语法)

(1)为WeeklySetting创建一个编辑器模板:

@model Settings.WeeklySetting
@Html.CheckBoxFor(m => m.IsChecked)
@Html.LabelFor(m => m.IsChecked,Model.DisplayName)
@Html.HiddenFor(m => m.DisplayName)
@Html.HiddenFor(m => m.Value)

(2) 使用 EditorFor 在您的视图中渲染(无 foreach):

@using (Html.BeginForm())
{
    <div>
        @Html.EditorFor(m => m.Settings)
    </div>
    <br />
    <input value="GenerateForWeekly" name="submitButton" type="submit" />
}
于 2012-07-06T11:38:07.530 回答
1

如果您使用for循环而不是foreach. 如此处所示:http: //haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

于 2012-07-06T20:02:59.593 回答