大家晚上好,我希望,任何人都可以在视图部分帮助我使用日期时间范围过滤器。这是我的模特:
public class Student
{
public int ID { get; set; }
public string StudentName { get; set; }
public int CourseId { get; set; }
public virtual Course Course { get; set; }
public DateTime CurrentDate { get; set; }
public Student()
{
CurrentDate = DateTime.Now;
}
}
我使用视图模型进行显示,现在这里是我的控制器:
public ActionResult Index(DateTime? startdate, DateTime? enddate)
{
var rangeData = db.Students.Where(x => x.CurrentDate >= startdate && x.CurrentDate <= enddate).ToList();
return View(rangeData);
}
现在我在视图和控制器方面都遇到了一些问题。
以下是我的问题:如何将开始和结束日期传递给控制器以获取具有已定义属性的订单?这是我的观点以及我做错了什么?
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("Index", "Students", 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>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.StudentName)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.MobileNo)
</th>
<th>
@Html.DisplayNameFor(model => model.Course)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.StudentName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.MobileNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.Course.CourseName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
答案 0 :(得分:0)
为输入字段提供占位符属性以指导用户使用预期的日期格式是您可以使用的一个选项,因为这似乎是日期格式问题。另一种选择是使用日期选择器控件,它将以正确的格式自动设置日期。
但是,如果您想让您的用户灵活地以任何格式选择输入日期,包括“/”,“ - ”或只是空格,这里有一些提示
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.Students.Where(x => x.CurrentDate >= start && x.CurrentDate <= end).ToList();
return View(rangeData);
}
return View();
}
public class DateManager
{
/// <summary>
/// Use to prevent month from being overritten when day is less than or equal 12
/// </summary>
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
{
//We are silent about this but we could set a message about wrong date input in ViewBag and display to user if this this method returns null
}
}
}
return null;
}
// Another overload. this will catch more date formats without manually checking as above
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;
}
}