我有一个具有多种搜索功能的发票应用程序(按类型,状态和客户ID搜索发票)。我只需要按年份实现搜索,并按日期选择器实现其他搜索(对于两个单独的视图)。有人可以帮我吗?
这是我的代码:
控制器:
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>
}
答案 0 :(得分:0)
将Year
属性添加到InvoiceViewModel
类,并通过向属性类型添加DateFrom
使DateTo
和?
为空
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);
}