繁体   English   中英

我需要帮助,因为我不明白为什么我的代码没有按日期执行过滤? ASP.NET

[英]I need help because I don't understand why my code is not performing filtering by date? ASP.NET

当我执行过滤时,没有任何值返回给我

我的视图索引页

我认为问题出在我的 ActionResult 索引中,但我不知道它是什么,因为我认为引用是正确的。我在索引控制器中知道的是将变量 DataTime 转换为 String

我的模特

public Nullable<System.DateTime> Data_Registo { get; set; }
public Programa()
    {
        Data_Registo = DateTime.Now;
    }

我的控制器:

public ActionResult Index(string startdate = null, string enddate = null)
    {
        if (startdate != null && enddate != null)
        {
            //this will default to current date if for whatever reason the date supplied by user did not parse successfully

            DateTime start = DateManager.GetDate(startdate) ?? DateTime.Now;

            DateTime end = DateManager.GetDate(enddate) ?? DateTime.Now;

            var rangeData = db.Programa.Where(x => x.Data_Registo >= start && x.Data_Registo <= end).ToList();

            return View(rangeData);
        }
        return View(db.Programa);
    }


    public class DateManager
    {

        static bool IsMonthAssigned { get; set; }



        public static DateTime? GetDate(string d)
        {
            char[] splitsoptions = { '/', '-', ' ' };
            foreach (var i in splitsoptions)
            {
                var y = 0;
                var m = 0;
                var day = 0;
                if (d.IndexOf(i) > 0)
                {
                    try
                    {
                        foreach (var e in d.Split(i))
                        {


                            if (e.Length == 4)
                            {
                                y = Convert.ToInt32(e);

                                continue;
                            }
                            if (Convert.ToInt32(e) <= 12 && !IsMonthAssigned)
                            {
                                m = Convert.ToInt32(e);
                                IsMonthAssigned = true;
                                continue;
                            }
                            day = Convert.ToInt32(e);


                        }

                        return new DateTime(y, m, day);
                    }
                    catch
                    {

                    }
                }
            }
            return null;


        }


        public static DateTime? GetDate(string d, bool custom)
        {
            CultureInfo culture = new CultureInfo("en-US");

            string[] dateFormats =
            {
            "dd/MM/yyyy", "MM/dd/yyyy", "yyyy/MM/dd", "yyyy/dd/MM", "dd-MM-yyyy", "MM-dd-yyyy", "yyyy-MM-dd",
            "yyyy-dd-MM", "dd MM yyyy", "MM dd yyyy", "yyyy MM dd", "yyyy dd MM", "dd.MM.yyyy", "MM.dd.yyyy",
            "yyyy.MM.dd", "yyyy.dd.MM","yyyyMMdd","yyyyddMM","MMddyyyy","ddMMyyyy"
        };//add your own to the array if any

            culture.DateTimeFormat.SetAllDateTimePatterns(dateFormats, 'Y');

            if (DateTime.TryParseExact(d, dateFormats, culture, DateTimeStyles.None, out var date))
                return date;

            return null;


        }
    }

我的看法

@using (Html.BeginForm("Index", "Programas", FormMethod.Get))
{
<fieldset>
    <legend>Search criteria</legend>
    @Html.Label("StartDate", "Start Date:")
    <input class="startdate" id="startdate" name="startdate" type="date" value="">
    @Html.Label("enddate", "End Date:")
    <input class="startdate" id="enddate" name="enddate" type="date" value="">
    <input type="submit" value="Apply" />
</fieldset>
}

(更新)我尝试创建直接条目,但未从数据库加载数据

        public ActionResult Index(DateTime? start, DateTime? end)
    {

            var programas = db.Programa.Where(x => x.Data_Registo >= start && x.Data_Registo <= end).ToList();

        return View(programas.ToList());


    }

感谢帮助

您的数据库应该允许日期时间列已经以正确的日期和时间格式存储数据。 你不应该用不同的格式来解析它。 然后,当您查询它并想要这些特定部分时,有日期时间函数可以从中提取诸如年、月、日、小时、分钟、秒之类的内容。 在您拥有数据后,打印/显示输出将成为格式化的一部分。 您也没有指明您正在使用哪个数据库。 对该数据库和日期时间函数进行搜索,你会看到很多东西...... MySQL、SQL-Server、Oracle 等。

我终于设法在日期之间进行过滤,就像尽可能简单一样。 我在下面留下代码,所以你需要按日期过滤。

public ActionResult Index(string startdate = null, string enddate = null)
    {
        if (startdate != null && enddate != null)
        {

            DateTime start = Convert.ToDateTime(startdate);

            DateTime end = Convert.ToDateTime(enddate);

            var rangeData = db.YourTable.Where(x => x.YourColumn >= start && x.YourColumn<= end).ToList();

            return View(rangeData);
        }
        return View(db.YourTable.ToList());
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM