【问题标题】:I need to implement SEARCH BY YEAR and SEARCH WITH DATE PICKER我需要实现 SEARCH BYEAR 和 SEARCH WITH DATE PICKER
【发布时间】:2018-09-25 10:05:43
【问题描述】:

我有一个具有多种搜索功能的发票应用程序(按类型、状态和客户 ID 搜索发票)。我只需要按年份实现搜索,并按 datepicker 进行额外搜索(对于两个单独的视图)。有人可以帮我吗?

这是我的代码:

控制器:

public ActionResult Search(InvoiceViewModel invoiceViewModel)
    {
        LoadStatus();
        LoadInvoiceType();

        invoiceViewModel.CustomerModelList = customerRepository.All();
        invoiceViewModel.InvoiceList = invoiceRepository.All()
            .WhereIf(!String.IsNullOrEmpty(invoiceViewModel.InvoiceCode),x=> x.InvoiceCode == invoiceViewModel.InvoiceCode)
            .WhereIf(!String.IsNullOrEmpty(invoiceViewModel.Type), x => x.Type == invoiceViewModel.Type)
            .WhereIf(!String.IsNullOrEmpty(invoiceViewModel.Status), x => x.Status == invoiceViewModel.Status)
             .WhereIf(invoiceViewModel.CustomerId.HasValue, x => x.CustomerId == invoiceViewModel.CustomerId.Value)
             .Include(x => x.CustomerModel).OrderBy(x => x.Id).ToList();

        invoiceViewModel.count = invoiceViewModel.InvoiceList.Count();
        return View("Index",invoiceViewModel);
    }

Index.cshtml:

<script>    
$(function () {
    $(".mydatepicker").datepicker();

    $(document).on('click', '.panel-heading span.clickable', function (e) {
        var $this = $(this);
        if (!$this.hasClass('panel-collapsed')) {
            $this.parents('.panel').find('.panel-body').slideUp();
            $this.addClass('panel-collapsed');
            $this.find('i').removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
        } else {
            $this.parents('.panel').find('.panel-body').slideDown();
            $this.removeClass('panel-collapsed');
            $this.find('i').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-up');
        }
    });
});    
</script>

@using (Html.BeginForm("Search", "Invoice", FormMethod.Get))
{
<div class="form-horizontal">
    <div class="col-md-2">
        @Html.EditorFor(model => model.InvoiceCode, new { htmlAttributes = new { @class = "form-control", @id = "InvoiceCode", @placeholder = "Invoice code" } })
    </div>
    <div class="col-md-2">
        @Html.DropDownListFor(model => model.CustomerId, new SelectList(Model.CustomerModelList, "Id", "Name"), "select Customer", new { @class = "form-control", @id = "CustomerId" })
    </div>
    <div class="col-md-2">
        @Html.DropDownListFor(model => model.Type, ViewData["invoicetype"] as List<SelectListItem>, "select type", new { @class = "form-control", @id = "Type" })
    </div>
    <div class="col-md-2">
        @Html.DropDownListFor(model => model.Status, ViewData["country"] as List<SelectListItem>, "select status", new { @class = "form-control", @id = "Status" })
    </div>

    <div class="col-md-1">
        <div>
            @Html.EditorFor(model => model.DateFrom, new { htmlAttributes = new { @class = "form-control mydatepicker", @id = "DateFrom", @placeholder = "Date From" } })
        </div>
    </div>
    <div class="col-md-1">
        <div>
            @Html.EditorFor(model => model.DateTo, new { htmlAttributes = new { @class = "form-control mydatepicker", @placeholder = "Due Date", @id = "DueDate" } })
        </div>
    </div>

    <div class="col-md-1">
        <input type="submit" style="width: 100%" id="btnSubmit" value="Search" class="btn btn-primary" />
    </div>
    <div class="col-md-1">
        <input type="submit" style="width: 100%" id="btnSubmit" value="Reset" class="btn btn-primary" />  
    </div>
</div>
}

【问题讨论】:

    标签: asp.net asp.net-mvc search datepicker


    【解决方案1】:

    Year 属性添加到InvoiceViewModel 类,并通过将? 添加到属性类型使您的DateFromDateTo 可以为空

    public int? Year { get; set; }
    public DateTime? DateFrom { get; set; }
    public DateTime? DateTo { get; set; }
    

    将有效年份列表添加到ViewData

    ViewData["years"] = new List<SelectListItem> { /* add display and value here */ };
    

    添加DropDownList查看

    <div class="col-md-2">
        @Html.DropDownListFor(model => model.Year, ViewData["country"] as List<SelectListItem>, "select year", new { @class = "form-control", @id = "Year" })
    </div>
    

    将相关代码添加到您的控制器

    public ActionResult Search(InvoiceViewModel invoiceViewModel)
    {
        LoadStatus();
        LoadInvoiceType();
    
        invoiceViewModel.CustomerModelList = customerRepository.All();
        invoiceViewModel.InvoiceList = invoiceRepository.All()
            .WhereIf(!String.IsNullOrEmpty(invoiceViewModel.InvoiceCode),x=> x.InvoiceCode == invoiceViewModel.InvoiceCode)
            .WhereIf(!String.IsNullOrEmpty(invoiceViewModel.Type), x => x.Type == invoiceViewModel.Type)
            .WhereIf(!String.IsNullOrEmpty(invoiceViewModel.Status), x => x.Status == invoiceViewModel.Status)
            // search the year of the needed date
            .WhereIf(invoiceViewModel.Year.HasValue, x => x.[date].Year == invoiceViewModel.Year)
            // search the needed date range
            .WhereIf(invoiceViewModel.DateFrom.HasValue, x => x.[date] >= invoiceViewModel.DateFrom)
            .WhereIf(invoiceViewModel.DateTo.HasValue, x => x.[date] <= invoiceViewModel.DateTo)
    
            .WhereIf(invoiceViewModel.CustomerId.HasValue, x => x.CustomerId == invoiceViewModel.CustomerId.Value)
            .Include(x => x.CustomerModel).OrderBy(x => x.Id).ToList();
    
        invoiceViewModel.count = invoiceViewModel.InvoiceList.Count();
        return View("Index",invoiceViewModel);
    }
    

    【讨论】:

    • 感谢您的帮助,其他一切正常,除了我不明白应该在哪里添加 ViewData(在模型、控制器中)以及如何显示值?
    • 控制器中设置了ViewData,不知道你要显示什么值?
    • 值 - 我的意思是年份范围。抱歉,问题的格式不正确。
    • invoicetype 视图中使用DropDownListFor 的方式相同
    • 如果您的意思是获取数据本身,您可以从数据库中的已知日期开始,也可以手动将其放入列表中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多