我想以不同的方式报告日期

时间:2015-08-12 07:12:57

标签: c# html asp.net asp.net-mvc-5

从这张照片中可以看出,我有一个我没有报告时间的遗漏日期列表,当我点击一个日期时,我错过了我的'Rappotera tid',并填写了该日期。 enter image description here

但我想要做的是我想删除我的'基准',它是基于日期之前的复选框,所以在以后可以一次报告几个日期。

但我被困住了,我想得到一些帮助。

这是我的观点:

    <script type="text/javascript" language="javascript">
    $(function() {
        $(function() {
            $('#date').datepicker({
                showButtonPanel: true,
                changeMonth: true,
                changeYear: true,
                dateFormat: "yy-mm-dd",
                firstDay: 1,
                onSelect: function(dateText) {
                    $('#EndDate').datepicker('option', 'minDate', new Date(dateText));
                }
            });
        });
    });

    function SetDate(dt) {
        $('#date').val(dt);
    }

    var n = @(Model.Projects.Count);;

    function AddProject() {
        n++;
        $.ajax({
            type: "GET",
            url: "@Url.Action("Project")/" + n,
            dataType: "html",
            success: function(data) {
                $('#projects').append(data);

            }
        });
    }

    $(function() {
        $('#startTime').change(function() { CalculateTime(); });
        $('#endTime').change(function() { CalculateTime(); });
        $('#breakTime').change(function() { CalculateTime(); });
        CalculateTime();
    });


    function CalculateTime() {
        try {
            var startTime = $('#startTime').val();
            var endTime = $('#endTime').val();
            var breakTime = $('#breakTime').val();

            var startDate = new Date(2000, 1, 1, startTime.substring(0, 2), startTime.substring(3, 5), 0, 0);
            var endDate = new Date(2000, 1, 1, endTime.substring(0, 2), endTime.substring(3, 5), 0, 0);

            var time = endDate - startDate;

            time = time / 1000 / 60 / 60;
            time = time - breakTime.substring(0, 2);
            time = time - (breakTime.substring(3, 5) / 60);
            $('#workedHours').html(time + " timmar");
        } catch (err) {
            $('#workedHours').html("---");
        }
    }
</script>
<div class="page-container">
    <div class="page-content">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    @if (ViewData["posted"] != null)
                    {
                        <div class="alert alert-success">
                            <strong>Tidsrapporten tillagd.</strong>
                        </div>
                    }
                    <div class="tabbable tabbable-custom tabbable-noborder tabbable-reversed">
                        <div class="tab-content">
                            <div class="portlet light bordered">
                                <div class="portlet-title">
                                    <div class="caption">
                                        <span class="caption-subject font-green-sharp bold uppercase">missad rappoterad tid</span>
                                    </div>
                                </div>
                                <form class="form-horizontal">
                                    <div class="portlet-body form">
                                        <div class="form-group">
                                            @foreach (var date in ViewBag.MissingDays)
                                            {
                                                var isoDate = date.ToString("yy-MM-dd");
                                                <div class="col-md-1">
                                                    <input type="checkbox" name="Date" value="Date">
                                                    <a href="javascript:SetDate('@isoDate');">@isoDate</a>
                                                </div>
                                            }
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

                @Html.ValidationSummary()
                @using (Html.BeginForm("TimeReport", "Reports", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal" }))
                {
                    @Html.Hidden("ReportId", Model.ReportId)
                    <div class="col-md-6">
                        <div class="tabbable tabbable-custom tabbable-noborder tabbable-reversed">
                            <div class="tab-content">
                                <div class="portlet light bordered">
                                    <div class="portlet-title">
                                        <div class="caption">
                                            <span class="caption-subject font-green-sharp bold uppercase">Rappotera tid</span>
                                        </div>
                                    </div>
                                    <div class="portlet-body form">
                                        <div class="form-group">
                                            <label class="col-md-3 control-label">Datum:</label>
                                            <div class="col-md-5">
                                                @Html.TextBox("date", Model.Date.ToShortDateString(), new {@class = "form-control"})
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-3 control-label">Start tid:</label>
                                            <div class="col-md-5">
                                                @Html.TextBox("startTime", Model.Times.StartTime, new { @class = "form-control timepicker timepicker-24" })
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-3 control-label">Slut tid:</label>
                                            <div class="col-md-5">
                                                @Html.TextBox("endTime", Model.Times.EndTime, new { @class = "form-control timepicker timepicker-24" })
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-3 control-label">Rast Längd:</label>
                                            <div class="col-md-5">
                                                @Html.TextBox("breakTime", Model.Times.BreakTime, new { @class = "form-control timepicker timepicker-24" })
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-3 control-label">Tid jobbad:</label>
                                            <div class="col-md-5">
                                                @Html.TextBox("workedHours", Model.Times.WorkedHours, new { @class = "form-control timepicker timepicker-24" })
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div id="projects">
                            @foreach (var data in Model.Projects)
                            {
                                Html.RenderPartial("Project", data, ViewData["vd"] as ViewDataDictionary);
                                var viewDataDictionary = ViewData["vd"] as ViewDataDictionary;
                                if (viewDataDictionary != null)
                                {
                                    viewDataDictionary["id"] = (int)viewDataDictionary["id"] + 1;
                                }
                            }
                        </div>
                    </div>
                    <div class="form-actions">
                        <div class="col-md-offset-4 col-asdfasmd-9">
                            <a href="javascript:AddProject()" class="btn btn-primary">Lägg till projekt</a>
                            <button type="submit" class="btn btn-primary">Spara</button>
                        </div>
                    </div>
                    if (Model.ReportId.HasValue)
                    {
                        <input type="submit" value="Ta bort" name="delete" />
                    }
                }
            </div>
        </div>
    </div>
</div>

这是我对此视图的控制器:

        public ActionResult TimeReport(FormCollection form, Guid? id)
    {
        ViewDataDictionary vd = new ViewDataDictionary
        {
            ["projects"] = new DatabaseLayer().GetConsultantProjects(Constants.CurrentUser(User.Identity.Name)),
            ["id"] = 1,
            ["showDescription"] = true
        };
        ViewData["vd"] = vd;

        NewTimeReportModel projectData = new NewTimeReportModel();

        if (form != null && form.AllKeys.Contains("delete"))
        {
            new DatabaseLayer().DeleteTimeReport(Guid.Parse(form["ReportId"]));
            LoadDefaultSettings(projectData);
            ViewData.Model = projectData;
            return View();
        }

        if (id.HasValue && (form == null || form.AllKeys.Length == 0))
        {
            using (DatabaseLayer db = new DatabaseLayer())
            {
                var timeReport = db.GetTimeReport(id.Value);
                projectData = new NewTimeReportModel(timeReport);
                if (projectData.Projects.Count == 1)
                    projectData.Projects[0].Hours = null;
            }
        }
        else if (form == null || form.AllKeys.Length == 0)
        {
            LoadDefaultSettings(projectData);
        }
        else
        {
            DateTime reportDate;
            if (!DateTime.TryParse(form["date"], out reportDate))
                ModelState.AddModelError("Date", "Felaktikt datum");

            var projectNumbers = (from x in form.AllKeys
                                  where x.Contains("_")
                                  select x.Substring(x.IndexOf('_'))).Distinct();

            projectData.Times = new TimeReportTimes(form["startTime"], form["endTime"], form["breakTime"], ModelState);
            projectData.Date = reportDate;

            if (!projectNumbers.Any())
                ModelState.AddModelError("Projekt", "Inga projekt valda...");
            else
            {
                int emptyHours = 0;

                foreach (string projectNumber in projectNumbers)
                {

                    projectData.Projects.Add(new NewTimeReportModel.Project
                    {
                        Description = form["description" + projectNumber],
                        Hours = null,
                        ProjectId = Guid.Parse(form["project" + projectNumber])
                    });

                    string hourString = form["hours" + projectNumber];
                    if (string.IsNullOrEmpty(hourString))
                    {
                        emptyHours++;
                        projectData.Projects[projectData.Projects.Count - 1].Hours = projectData.Times.WorkedHours;
                    }
                    else
                    {
                        if (!projectData.Projects[projectData.Projects.Count - 1].SetHours(hourString))
                        {
                            ModelState.AddModelError("hours_" + projectNumber, "Felaktig antal timmar");
                        }
                    }
                }

                if (emptyHours > 1 || (emptyHours == 0 && projectData.Projects.Sum(x => x.Hours) != projectData.Times.WorkedHours))
                {
                    ModelState.AddModelError("hours_" + projectNumbers.First(), "Antalet timmar stämmer ej överrens");
                }

                if (projectData.Projects.Where(x => x.Hours <= 0).Any())
                {
                    ModelState.AddModelError("hours_" + projectNumbers.First(), "Antalet timmar måste vara större än noll");
                }

                if (!string.IsNullOrEmpty(form["ReportId"]))
                    projectData.ReportId = Guid.Parse(form["ReportId"]);

                if (ModelState.IsValid)
                {
                    projectData.SaveToDatabase(Constants.CurrentUser(User.Identity.Name));
                    ViewData["posted"] = true;

                    projectData = new NewTimeReportModel();
                    LoadDefaultSettings(projectData);
                }
                else if (projectData.Projects.Count == 1)
                    projectData.Projects[0].Hours = null;
            }

        }

        var missingdays = new DatabaseLayer().GetConsultantMissingDays(Constants.CurrentUser(User.Identity.Name));
        if (missingdays.Count == 0)
        {
            ViewData["missingDays"] = "";
        }
        else
        {
            ViewBag.MissingDays = missingdays;
        }

        ViewData.Model = projectData;

        return View();
    }

0 个答案:

没有答案