我想使用 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()) { %>
<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>