简体   繁体   English

ASP.Net Html.TextBox 与日期

[英]ASP.Net Html.TextBox with Dates

I am trying to pass 2 date parameters from the View to the Controller.我正在尝试将 View 中的 2 个日期参数传递给 Controller。

View看法

      <div class="col-md-4">
            <p>Date From:</p>
            @Html.TextBox("ExpDateFrom", ViewBag.ExpDateFrom as DateTime)
        </div>
        <div class="col-md-4">
            <p>Date To:</p>
            @Html.TextBox("ExpDateTo", ViewBag.ExpDateTo as DateTime)
            <input type="submit" value="Find" />
        </div>

Controller Controller

public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page, DateTime dateOfExposureFrom, DateTime dateOfExposureTo)

Obviously right now this isn't going to work.显然现在这行不通。 I'm not even sure if you should use Html.TextBox for this, but even if you can there is an issue with using DateTime because it is not nullable.我什至不确定您是否应该为此使用 Html.TextBox ,但即使您可以使用 DateTime 也存在问题,因为它不可为空。

How do I handle this?我该如何处理? If I need to completely rework my approach that is fine.如果我需要完全修改我的方法,那很好。 I just need to pass 2 date parameters from a view to a controller.我只需要将 2 个日期参数从视图传递到 controller。

Use reusable model - this will make things easier during further development.使用可重复使用的 model - 这将使进一步开发过程中的事情变得更容易。

public class SearchModel 
{
public string SortOrder { get; set; }
public string CurrentFilter { get; set; }
public string SearchString { get; set; }
public int? Page { get; set; }
public DateTime DateOfExposureFrom { get; set; }
public DateTime DateOfExposureTo { get; set; }
}

Then controller然后 controller

public ActionResult Index(SearchModel model)

Then view然后查看

@model SearchModel

<div class="col-md-4">
   <p>Date From:</p>
   @Html.TextBoxFor(m => m.DateOfExposureFrom, new { @class = "form-control" })
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM