When I perform the filtering, no value is returned to me
I think the problem is in my ActionResult Index but I don’t know what it is, because i think the references are correct.what I know in my index controller is converting the variable DataTime to String
My Model
public Nullable<System.DateTime> Data_Registo { get; set; }
public Programa()
{
Data_Registo = DateTime.Now;
}
My Controller:
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;
}
}
My View
@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>
}
(UPDATE)
I tried to create a direct entry but data is not loaded from the database
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());
}
Thanks for help
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Your database SHOULD allow a datetime column that already stores data in a proper date and time format. You should not have to parse it all over in different formats. Then, when you query it and want those particular parts, there are datetime functions to extract things like year, month, day, hour, minute, second from it. And printing/displaying the output would be part of the formatting AFTER you have the data. You also do not indicate which database you are working with. Do a search on that database plus datetime functions and you’ll see a lot of stuff… MySQL, SQL-Server, Oracle, etc.
Method 2
I finally managed to filter between dates, nothing like being as simple as possible.
I leave code below so you need to filter by dates.
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());
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0