3

我有一个有 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);
        }
    }
4

1 回答 1

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>应该没问题)才能使用索引器,但这不应该是我想象的问题。发布时,集合应正确填写。

于 2012-10-26T21:48:57.343 回答