【问题标题】:Filtering data according to date range mvc4根据日期范围 mvc4 过滤数据
【发布时间】: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


【解决方案1】:

您收到的错误消息表明没有将 StartDate 参数的值传递给控制器​​上的 List 操作。您从哪里调用此操作?确保传递 StartDate 和 EndDate 的值。例如:

@Html.ActionLink("List Cards", "List", "Cards", new { StartDate = DateTime.Today.Date, EndDate = DateTime.Today.Date.AddDays(7) }, null)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-10
    • 2016-11-25
    • 2020-02-15
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    相关资源
    最近更新 更多