【发布时间】:2014-08-14 22:57:08
【问题描述】:
我正在根据日期范围过滤数据库中的数据。
第一张桌子卡片1: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
我应该如何解决这个问题。我想从日期选择器中选择日期然后过滤。
请帮助我。提前谢谢你
【问题讨论】:
-
机会是您的日期选择器,您尝试通过的文化将不受约束。当您将
startDate和endDate参数改为string(在操作参数中)时会发生什么? -
我们可以看看你如何调用action方法的代码吗?
-
我只有上面写的这段代码。
标签: c# linq asp.net-mvc-4 filter