【问题标题】:DateTime Parameters in MVCMVC 中的日期时间参数
【发布时间】:2015-02-12 17:15:56
【问题描述】:

我正在开发一个程序,我将一个DateTime 变量发送到一个可排序的jQuery DataTable。 这是我的“模型”文件中的构造函数:

 public StudentDailyStatus(int studentID, DateTime startDate, DateTime endDate, string code, string status, int goodDays, int badDays, int leave, int lengthOfStay)

然后这里是我试图放入对象的值:

 new StudentDailyStatus(1, (2015, 1, 1), (2015, 2, 1), "1","Normal","2",0,0,0),

我的错误是:“[文件] 不包含接受 4 个参数的构造函数”。

但我遇到了startDate 和endDate 参数的问题。如何使用DateTime 变量? 感谢您的帮助!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using SAAS.Models;

namespace SAAS.Services
{
    public class StudentDailyStatusService
    {
        private static readonly List<StudentDailyStatus> StudentDailyStatuses;

        static StudentDailyStatusService()
        {
            StudentDailyStatuses = new List<StudentDailyStatus>
            {
                // StudentID, StartDate, EndDate, Code, Status, GoodDays, BadDays, Leave, LengthOfStay
                new StudentDailyStatus(1, new DateTime(2015, 1, 1), new DateTime(2015, 2, 1), "1","Normal",2,0,0,0),
                new StudentDailyStatus(1, new DateTime(2015, 1, 3), new DateTime(2015, 1, 4), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 5), new DateTime(2015, 1, 6), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 7), new DateTime(2015, 1, 8), "1","Normal",0,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 9), new DateTime(2015, 1, 10), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 11), new DateTime(2015, 1, 13), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 12), new DateTime(2015, 1, 14), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 15), new DateTime(2015, 1, 16), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 15), new DateTime(2015, 1, 16), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 19), new DateTime(2015, 1, 20), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 21), new DateTime(2015, 1, 22), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 23), new DateTime(2015, 1, 24), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 25), new DateTime(2015, 1, 26), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 29), new DateTime(2015, 1, 31), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 1, 31), new DateTime(2015, 2, 1), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 2), new DateTime(2015, 2, 3), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 4), new DateTime(2015, 2, 5), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 6), new DateTime(2015, 2, 7), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2105, 2, 8), new DateTime(2015, 2, 9), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 10), new DateTime(2015, 2, 11), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 12), new DateTime(2015, 2, 13), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 14), new DateTime(2105, 2, 15), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 16), new DateTime(2015, 2, 17), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 18), new DateTime(2015, 2, 19), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 20), new DateTime(2015, 2, 21), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 22), new DateTime(2015, 2, 23), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 24), new DateTime(2015, 2, 25), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 26), new DateTime(2015, 2, 27), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 2, 28), new DateTime(2015, 3, 1), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 2), new DateTime(2015, 3, 3), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 4), new DateTime(2015, 3, 5), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 6), new DateTime(2015, 3, 7), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 8), new DateTime(2015, 3, 9), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 10), new DateTime(2015, 3, 11), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 12), new DateTime(2015, 3, 13), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 12), new DateTime(2015, 3, 15), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 16), new DateTime(2015, 3, 17), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 18), new DateTime(2015, 3, 19), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 20), new DateTime(2015, 3, 21), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 22), new DateTime(2015, 3, 23), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 24), new DateTime(2015, 3, 25), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 26), new DateTime(2015, 3, 27), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 28), new DateTime(2015, 3, 29), "1","Normal",2,0,0,2),
                new StudentDailyStatus(1, new DateTime(2015, 3, 30), new DateTime(2015, 4, 2), "1","Normal",2,0,0,2)
            };
        }

        public List<StudentDailyStatus> GetStudentDailyStatuses(int start, int length)
        {
            return FilterStudentDailyStatuses().Skip(start).Take(length).ToList();
            //return StudentAllotments.ToList();
        }

        public int Count()
        {
            return FilterStudentDailyStatuses().Count();
        }

        public IQueryable<StudentDailyStatus> FilterStudentDailyStatuses()
        {
            IQueryable<StudentDailyStatus> results = StudentDailyStatuses.AsQueryable();

            return results;
        }
    }
}

填充表格的文件是:

@{
    ViewBag.Title = "Student - Daily Status";
    ViewBag.SubTitle = "Status History";
}

@Html.Partial("_StudentNavPartial")
@Html.Partial("_StudentSearchBar")
<div class="container-fluid left">
    <h4>@ViewBag.SubTitle</h4>

    <div class="container-fluid left">
        <table id="StudentDailyStatusTable" class="table table-striped table-bordered table-hover text-nowrap">
            <thead>
                <tr>
                    <th data-column="StudentID">Student ID</th>
                    <th data-column="StartDate" class="text-center" id="TDtooltip" data-toggle="tooltip" data-placement="bottom" data-original-title="Date Attendance Started" data-container="body">Start Date</th>
                    <th data-column="EndDate" class="text-center" id="TDtooltip" data-toggle="tooltip" data-placement="bottom" data-original-title="Date Attendance Ended" data-container="body">End Date</th>
                    <th data-column="Code" class="text-left" id="TDtooltip" data-toggle="tooltip" data-placement="bottom" data-original-title="Attendance Status Code" data-container="body">Code</th>
                    <th data-column="Status" class="text-center" id="TDtooltip" data-toggle="tooltip" data-placement="bottom" data-original-title="Attendance Status Code" data-container="body">Status</th>
                    <th data-column="GoodDays" class="text-center" id="TDtooltip" data-toggle="tooltip" data-placement="bottom" data-original-title="Payable Days for the Student" data-container="body">Good Days</th>
                    <th data-column="BadDays" class="text-left" id="TDtooltip" data-toggle="tooltip" data-placement="bottom" data-original-title="Leave Days Paid or Unpaid for the Student" data-container="body">Bad Days</th>
                    <th data-column="Leave" class="text-center" id="TDtooltip" data-toggle="tooltip" data-placement="bottom" data-original-title="Leave Days for which the Sudent is Not Paid" data-container="body">Leave<br /> Status</th>
                    <th data-column="LengthOfStay" class="text-center" id="TDtooltip" data-toggle="tooltip" data-placement="bottom" data-original-title="Days between Enrollment and Termination" data-container="body">Length of Stay</th>
                </tr>
            </thead>
        </table>
    </div>
</div>

@section scripts
{
    @Scripts.Render("~/bundles/dataTables")

    <script type="text/javascript">
        $.extend($.fn.dataTable.defaults, {
            'pagingType': 'full_numbers',
            'pageLength': 10,
            'language': {
                'paginate': {
                    'first': '&laquo;',
                    'last': '&raquo;',
                    'previous': '&lsaquo;',
                    'next': '&rsaquo;'
                },
                'info': 'Displaying _START_-_END_ of _TOTAL_',
                'infoEmpty': 'Displaying 0 to 0 of 0',
                'infoFiltered': '',
                'search': '',
                'zeroRecords': 'No matching records found'
            },
            //'dom': '<"top"f>rt<"bottom"<"pull-left"l><"pull-right"i>p<"clear">>',
            'dom': '<"top">rt<"bottom"<"pull-left"l><"pull-right"i>p<"clear">>',
            'initComplete': function (settings, json) {
                $('.dataTables_filter input[type=search]').attr('placeholder', 'Search');
            }
        });

        $(function () {

            //jQuery(oTable.fnGetNodes()).tooltip({ //tooltip plugin for jQuery!
            //    "delay": 250,
            //    "track": true
            //});

            $('#searchButton1').click(function () {
                var filtering = "";
                var number = 0;
                var filterType = "";
                if ($('#searchSsn').val() != '') {
                    filtering = $('#searchSsn').val();
                }
                else if ($('#searchStudentName').val() != '') {
                    filtering = $('#searchStudentName').val();
                    filterType = "Name";
                }
                else if ($('#searchStudentID').val() != '') {
                    filtering = $('#searchStudentID').val();
                    filterType = "StudentID";
                }
                //Search to get studentID number and count number of results and put into "number"
                if(number == 1)
                {
                    //Filter for the one studentID retrieved from the database
                }
                else if(number == 0)
                {
                    //Show the "no results found" window
                }
                else
                {
                    //Multiple results, show the dialog box that will allow the users to choose the correct student
                }
            });

            $('#StudentDailyStatusTable').dataTable({
                'ajax': {
                    type: 'POST',
                    url: '@Url.Action("GetData", "StudentDailyStatus")',
                    //data: function (d) {

                    //}
                },
                columns: [
                {
                    data: 'StudentID',
                    visible: false
                },
                {
                    data: 'StartDate',
                    sortable: true,
                    className: "text-center"
                },
                {
                    data: 'EndDate',
                    sortable: true,
                    className: "text-center"
                },
                {
                    data: 'Code',
                    sortable: true,
                    className: "text-center"
                },
                {
                    data: 'Status',
                    sortable: true,
                    className: "text-center"
                },
                {
                    data: 'GoodDays',
                    sortable: true,
                    className: "text-center"
                },
                {
                    data: 'BadDays',
                    sortable: true,
                    className: "text-center"
                },
                {
                    data: 'Leave',
                    sortable: true,
                    className: "text-center"
                },
                {
                    data: 'LengthOfStay',
                    sortable: true,
                    className: "text-center"
                }
                ],
                //jQueryUI: true,
                sort: true,
                ordering: true,
                order: [0, 'StartDate'],
                processing: true,
                serverSide: true
            });

            //$('#TDtooltip').tooltip({
            //    delay: 0,
            //    track: true,
            //    fade: 100
            //});
            //$('td').tooltip({
            //    delay: 0,
            //    track: true,
            //    fade: 100
            //});
            $('[data-toggle="tooltip"]').tooltip({
                delay: 0,
                track: true,
                fade: 100
            });

        });
    </script>

}

好的,这是控制器:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using SAAS.Models;
using SAAS.Services;

namespace SAAS.Controllers
{
    public class StudentDailyStatusController : Controller
    {

        // GET: StudentDailyStatus
        public ActionResult DailyStatus()
        {
            return View();
        }
        public JsonResult GetData(DTParameters dtModel, FilterViewModel filterModel)
        {
            try
            {
                List<StudentDailyStatus> data = new StudentDailyStatusService().GetStudentDailyStatuses(dtModel.Start, dtModel.Length);
                int count = new StudentDailyStatusService().Count();
                DTResult<StudentDailyStatus> result = new DTResult<StudentDailyStatus>
                {
                    draw = dtModel.Draw,
                    data = data,
                    recordsFiltered = count,
                    recordsTotal = count
                };
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new { error = ex.Message });
            }
        }

    }
}

这是“模型”文件:

using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

namespace SAAS.Models
{
    public class StudentDailyStatus
    {
        public StudentDailyStatus(int studentID, DateTime startDate, DateTime endDate, string code, string status, int goodDays, int badDays, int leave, int lengthOfStay)
        {
            StudentID = studentID;
            StartDate = startDate;
            EndDate = endDate;
            Code = code;
            Status = status;
            GoodDays = goodDays;
            BadDays = badDays;
            Leave = leave;
            LengthOfStay = lengthOfStay;
        }

        [Key]
        public int StudentID { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public string Code { get; set; }
        public string Status { get; set; }
        public int GoodDays { get; set; }
        public int BadDays { get; set; }
        public int Leave { get; set; }
        public int LengthOfStay { get; set; }

    }

}

【问题讨论】:

  • 看看你的错误,看看你写的代码。
  • @JustinRusso 代码为:'code'public StudentDailyStatus(int studentID, DateTime startDate, DateTime endDate, string code, string status, int goodDays, int badDays, int leave, int lengthOfStay)'code'和'code'然后这里是我试图放入对象的值:new StudentDailyStatus(1, (2015, 1, 1), (2015, 2, 1), "1","Normal"," 2",0,0,0)'code' 错误是:它没有采用带有 4 个参数的构造函数。所以我想我输入的日期是错误的
  • 您可以编辑您的原始帖子并将代码粘贴到他们的帖子中吗?
  • ...不要忘记包含您遇到的错误。
  • @JustinRusso 我刚刚做了

标签: c# jquery asp.net-mvc datetime


【解决方案1】:

好的,如果您要输入这样的日期,(2015, 2, 1),那您就错了。

您需要像这样将日期作为参数传递...new DateTime(2015, 2, 1)

所以,这应该是你的构造函数调用...

new StudentDailyStatus(1, new DateTime(2015, 1, 1), new DateTime(2015, 2, 1), "1","Normal","2",0,0,0)

【讨论】:

  • 好的,谢谢!!有没有办法做到这一点,但要格式化?当我运行程序并使用 jQuery 获取日期值以将其放入表中时,StartDate 和 EndDate 列填充了如下所示的值:/Date(1420088400000)/。关于如何使其以 mm/dd/yyyy 格式显示的任何想法?谢谢!
  • 是的,当您将日期变量中的值分配给控件时,您会执行“.ToString”。所以只需传入一个格式... dateTime.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
  • 这种东西在这个小块中是如何工作的:{data: 'StartDate', sortable: true, className: "text-center"},?我正在将我的 DataTables 填充到这样的一堆块中,但在这种情况下我无法确定格式...
  • 您能否将“StudentDailyStatus”构造函数方法作为“编辑”发布到您的原始帖子中?我想看看你在做什么。
  • 当然!它只是一个占位符,因为最终我们将从数据库中获取数据,但它的想法和变量类型是相同的
【解决方案2】:

您只是发送 ("2015,01,...") 而不是日期。您必须先创建一个 dateTime 对象,然后发送给您的构造函数。

new Date(year, month, day, hours, minutes, seconds, milliseconds);

更多参考请查看http://www.w3schools.com/jsref/jsref_obj_date.asp

【讨论】:

  • 仅供参考 - 正确的想法,但您的参考是针对 JS 的......有问题的代码是 C#。
【解决方案3】:

(2015, 1, 1) 不是 DateTime 值...改用 new DateTime(2015, 1, 1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多