我根据日期范围从数据库中过滤数据。
First Table Card1:CardId, Description
。 - 保留所有可用卡的记录第二表Registartion1 : RegistartionId, RegisteredDateTime , CardId
。保留使用特定卡的时间和数据数据。
在我的卡控制器中,我有以下代码。
public ActionResult List(DateTime startDate, DateTime endDate)
{
using (var db = new CardDBEntities())
{
var cards = (from c in db.Card1
join d in db.Registration1
on c.CardId equals d.CardId
where d.RegistrationDateTime >= startDate &&
d.RegistrationDateTime <= endDate
select new ModelN
{
CardId = d.CardId,
Description = c.Description,
RegistrationDateTime = d.RegistrationDateTime
}).OrderByDescending(x => x.RegistrationDateTime)
.ToList();
ViewData["cards"] = cards;
return View();
}
我的新模特是:
public class ModelN
{
public int CardId { get; set; }
public string Description { get; set; }
public DateTime RegistrationDateTime { get; set; }
}
在我看来,我有这个:
<script>
$(function() {
$( "#startDate" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
onClose: function( selectedDate ) {
$( "#endDate" ).datepicker( "option", "minDate", selectedDate );
}
});
$("#endDate").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
onClose: function( selectedDate ) {
$("#startDate").datepicker("option", "maxDate", selectedDate);
}
});
});
</script>
<body>
<label for="startDate">From</label>
<input type="text" id="startDate" name="from">
<label for="endDate">to</label>
<input type="text" id="endDate" name="to">
</body>
当我运行该程序时,我得到了这个消息:
The parameters dictionary contains a null entry for parameter 'startDate' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult List(System.DateTime, System.DateTime)' in 'CardReader.Controllers.CardController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
我该如何解决这个问题。我想从datepicker中选择日期然后过滤。
请帮帮我。提前谢谢
答案 0 :(得分:0)
您收到的错误消息表明StartDate参数的值没有传递给控制器上的List操作。你在哪里称这个动作?确保传递StartDate和EndDate的值。例如:
@Html.ActionLink("List Cards", "List", "Cards", new { StartDate = DateTime.Today.Date, EndDate = DateTime.Today.Date.AddDays(7) }, null)