所以在真正停下来思考这个错误之后,我已经解决了我的问题。
好吧,实际上有两个我会解释的。
我收到的错误是:
System.Web.httpException:“DataBinding:”ShiftPatternConfigurator.Models.Month“不包含名为“Month”的属性。”
这让我想到一个事实,我的枚举没有DataValueField或DataField,因为它是一个枚举。
因此,我查看了创建selectlist的代码:
ViewBag.Month = new SelectList(Enum.GetValues(typeof(Month)), "Month", "Month", DateTime.Now.ToString("MMMM"));
我更仔细地研究了重载的方法,发现其中一个方法不需要dataValueField或datatypeField,因此我将代码修改为以下内容:
ViewBag.Month = new SelectList(Enum.GetValues(typeof(Month)), DateTime.Now.ToString("MMMM"));
这样就可以呈现DropDownList。但是,我仍然遇到了一个未设置所选值的问题。在快速搜索之后,我发现你不能将viewbags动态字段设置为与models属性相同。
所以我修改了控制器代码如下:
ViewBag.MonthFilter = new SelectList(Enum.GetValues(typeof(Month)), DateTime.Now.ToString("MMMM"));
我的视图代码是这样的:
@using (Html.BeginForm("SelectMonth", "HomeController", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.DropDownList("Month", (SelectList)ViewBag.MonthFilter)
<input type="submit" value="SelectMonth" />
}
编辑:这是我使用@gregh提供的答案后更新的新代码
模型:
using System;
using System.Collections.Generic;
namespace ShiftPatternConfigurator.Models
{
// view model
public class ShiftViewModel
{
public IEnumerable<ShiftModel> Shifts { get; set; }
public Month Month { get; set; }
}
// shift model
public class ShiftModel
{
public int ShiftNo;
public string ShiftName;
public DateTime StartTime;
public DateTime FinishTime;
public string Team;
public int Week;
public int CycleWeek = 0;
public string StartDay;
public DateTime StartDate;
}
// month enum
public enum Month
{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12
}
}
控制器:
using Oracle.ManagedDataAccess.Client;
using ShiftPatternConfigurator.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
namespace ShiftPatternConfigurator.Controllers
{
public class HomeController : Controller
{
// GET: Index
public ActionResult Index()
{
ViewBag.Title = "Shift Pattern Configurator Test";
ShiftViewModel view = new ShiftViewModel
{
Month = new Month()
};
view.Month = (Month)DateTime.Now.Month;
view.Shifts = GetShiftData(view.Month);
return View(view);
}
[HttpPost]
public ActionResult Index(ShiftViewModel view)
{
ViewBag.Title = "Shift Pattern Configurator";
view.Shifts = GetShiftData(view.Month);
return View(view);
}
private List<ShiftModel> GetShiftData(Month month)...
}
}
观点:
@model ShiftPatternConfigurator.Models.ShiftViewModel
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.EnumDropDownListFor(x => x.Month)
<input type="submit" value="SelectMonth" />
}
<table class="table">
<tr>
<th>Shift No</th>
<th>Shift Name</th>
<th>Start Time</th>
<th>Finish Time</th>
<th>Team</th>
<th>Week</th>
<th>Start Day</th>
<th>Start Date</th>
<th></th>
</tr>
@if (Model.Shifts.Count() > 0)
{
foreach (var item in Model.Shifts)
{
<tr>
<td>@item.ShiftNo</td>
<td>@item.ShiftName</td>
<td>@item.StartTime</td>
<td>@item.FinishTime</td>
<td>@item.Team</td>
<td>@item.Week</td>
<td>@item.StartDay</td>
<td>@item.StartDate.ToShortDateString()</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
}
else
{
<tr>
<td colspan="10" align="center"><h2>No Data</h2></td>
</tr>
}
</table>