-2
When I do 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 to convert the Datatime variable into 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 the 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
What exactly is your question? if the database is not returning any records, how do you expect to have something to display?
– Leandro Angelo
My database does not return any record after applying the filter. The question is what can be wrong not to return any record? Note: before the filter the right BD is loaded, only after applying the filter it returns nothing.
– Miguel
Make the query in hand, try other parameters with a larger range. You may be having trouble finding results because of the time next to the date
– Leandro Angelo