2 つの日付ピッカーを持つビューがあります。1 つは開始日で、もう 1 つは終了日です。ユーザーに開始日と終了日を選択させ、ユーザーが日付を変更し、時間を入力できる時間作業のリストを持つビューにリダイレクトし、完了したらクリックできるようにすることができますボタンを保存すると、データベースに対して作業するたびに新しいインスタンスが作成されます。ビューは問題なく表示されましたが、フォームが列挙可能なモデルを null として投稿すると、フォーム コレクションにはアクセス可能なアイテムが 1 つしかなく、その理由がわかりません。以下で作成しているビューを見て、私が何をしているのかを理解してください。何らかの形で私を助けることができるかどうかをお知らせください.
@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);
}
}