祝大家晚上好,我希望任何人都可以在视图部分帮助我使用日期时间范围过滤器。这里是我的模型:asp.net mvc 5中的日期范围搜索过滤器功能?
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>
为什么你有2个独立的表格和第二个控制器的方法。您的所有控件应该采用一种形式并发布到第一种方法。并且不要创建像这样的输入。使用带有这些属性的视图模型('SearchBy','StartDate'等等,并用'List'属性为过滤的集合 –
请问您可以通过代码在控制器中发布并查看 –
确切的问题和控制器? – Ravi