保存 IEnumerable 模型
Posted
技术标签:
【中文标题】保存 IEnumerable 模型【英文标题】:Saving an IEnumerable Model 【发布时间】:2012-10-26 19:38:56 【问题描述】:我的视图有 2 个日期选择器,其中一个是开始日期,另一个是结束日期。我能够让用户选择开始和结束日期,重定向到一个视图,该视图将包含一个工作时间列表,用户可以在其中更改日期、输入时间,一旦完成,他们应该能够单击保存按钮,它应该创建一个每次工作到数据库的新实例。我的视图显示没有问题,但是当表单发布我的可枚举模型为空时,表单集合只有一个可访问的项目,我不知道为什么。请查看我在下面创建的视图,以了解我在做什么,如果您能以任何方式帮助我,请告诉我。
@model IEnumerable<TimeCollection.Models.TimeWorked>
@
ViewBag.Title = "Create TimeWorked Range";
<h2>Create TimeWorked Range</h2>
@using (html.BeginForm())
@Html.ValidationSummary(true)
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.WorkDate)
</th>
<th>
@Html.DisplayNameFor(model => model.HoursWorked)
</th>
</tr>
@foreach (var item in Model)
<tr>
@Html.HiddenFor(modelItem => item.EmployeeId)
<td>
@Html.EditorFor(modelItem => item.WorkDate)
</td>
<td>
@Html.EditorFor(modelItem => item.HoursWorked)
</td>
<td>
@Html.HiddenFor(modelItem => item.BeenSubmitted)
</td>
</tr>
</table>
<p>
<input type="submit" value="Save" />
</p>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TimeCollection.Models
public class TimeWorked
public int TimeWorkedId get; set;
public int EmployeeId get; set;
public DateTime WorkDate get; set;
public int HoursWorked get; set;
public bool BeenSubmitted get; set;
public static void CreateTimeWorked(TimeWorked timeWorkedToCreate)
string insertQuery = string.Format("insert into time_worked (employee_id, work_date, work_hours, been_submitted) values ('0', '1', '2', '3')", timeWorkedToCreate.EmployeeId, timeWorkedToCreate.WorkDate.ToString("yyyy/MM/dd"), timeWorkedToCreate.HoursWorked, (timeWorkedToCreate.BeenSubmitted == true ? "1" : "0"));
SpectrumData.Utility.ExecutemysqlCommand(SpectrumData.Properties.Resources.SpectrumTSDatabaseConnectionString, insertQuery);
public static TimeWorked ReadTimeWorked(int timeWorkedId)
string selectQuery = string.Format("select * from time_worked where time_worked_id = '0'", timeWorkedId);
return ConvertDataRowIntoTimeWorked(SpectrumData.Utility.FillDataSet(SpectrumData.Properties.Resources.SpectrumTSDatabaseConnectionString, selectQuery).Tables[0].Rows[0]);
public static void UpdateTimeWorked(TimeWorked timeWorkedToUpdate)
string updateQuery = string.Format("update time_worked set work_date = '0', work_hours = '1', been_submitted = '2' where time_worked_id = '2'", timeWorkedToUpdate.WorkDate, timeWorkedToUpdate.HoursWorked, timeWorkedToUpdate.BeenSubmitted, timeWorkedToUpdate.TimeWorkedId);
SpectrumData.Utility.ExecuteMySqlCommand(SpectrumData.Properties.Resources.SpectrumTSDatabaseConnectionString, updateQuery);
public static void DeleteTimeWorked(int timeWorkedId)
string deleteQuery = string.Format("delete from time_worked where time_worked_id = '0'", timeWorkedId);
SpectrumData.Utility.ExecuteMySqlCommand(SpectrumData.Properties.Resources.SpectrumTSDatabaseConnectionString, deleteQuery);
private static TimeWorked ConvertDataRowIntoTimeWorked(System.Data.DataRow timeWorkedDataRow)
TimeWorked timeWorked = new TimeWorked();
timeWorked.BeenSubmitted = (timeWorkedDataRow["been_submitted"].ToString() == "1" ? true : false);
timeWorked.EmployeeId = int.Parse(timeWorkedDataRow["employee_id"].ToString());
timeWorked.HoursWorked = int.Parse(timeWorkedDataRow["work_hours"].ToString());
timeWorked.TimeWorkedId = int.Parse(timeWorkedDataRow["time_worked_id"].ToString());
timeWorked.WorkDate = DateTime.Parse(timeWorkedDataRow["work_date"].ToString());
return timeWorked;
public ActionResult CreateTimeWorkedRange(DateTime startDate, DateTime endDate)
List<Models.TimeWorked> listOfTimeWorked = new List<Models.TimeWorked>();
DateTime beginning = startDate;
DateTime ending = endDate;
while (beginning <= ending)
Models.TimeWorked dayWorked = new Models.TimeWorked()
EmployeeId = (Session["InventoryReviewUser"] as SpectrumData.SpectrumTS.InventoryReviewUser).EmployeeId,
WorkDate = beginning
;
if (listOfTimeWorked.Contains(dayWorked) == false)
listOfTimeWorked.Add(dayWorked);
beginning = beginning.AddDays(1);
return View(listOfTimeWorked);
[HttpPost]
public ActionResult CreateTimeWorkedRange(List<Models.TimeWorked> modelList)
foreach (Models.TimeWorked timeWorked in modelList)
Models.TimeWorked.CreateTimeWorked(timeWorked);
【问题讨论】:
【参考方案1】:我不知道是否有更好的方法,但一种方法是使用数组的索引绑定您的项目,如下所示:
@for (int i = 0; i < Model.Count; ++i)
<tr>
@Html.HiddenFor(modelItem => Model[i].EmployeeId)
<td>
@Html.TextBoxFor(modelItem => Model[i].WorkDate)
</td>
<td>
@Html.TextBoxFor(modelItem => Model[i].HoursWorked)
</td>
<td>
@Html.HiddenFor(modelItem => Model[i].BeenSubmitted)
</td>
</tr>
您的模型需要不同的集合类型(IList<TimeWorked>
应该没问题)才能使用索引器,但这不应该是我想像的问题。发布时,集合应正确填写。
【讨论】:
以上是关于保存 IEnumerable 模型的主要内容,如果未能解决你的问题,请参考以下文章
在 JavaScript 中遍历 IEnumerable 模型
asp.net core如何一一显示IEnumerable模型
错误:传入字典的模型项的类型为“....List”,但此字典需要“...IEnumerable”类型的模型
传入字典的模型项的类型是 .. 但此字典需要 System.Collections.Generic.IEnumerable' 类型的模型项