当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Filter on Multiselect listbox user selected values using MVC 4

问题描述:

I have been trying to figure out for days how to filter on my multiselect listbox in MVC with my if statements. I have all of my search fields working but the multiselect listbox will only filter the first selected option even if more are selected. I am using HTTPGET as this is only for filtering the results on webpage. Here is my controller code:

using System;

using System.Linq;

using System.Web.Mvc;

using accrual.Models.DB;

using pcard.Models.ViewModel;

using PagedList;

using System.Data;

using System.Globalization;

using System.Collections.Generic;

using System.Windows.Forms;

using System.Collections;

namespace pcard.Controllers

{

public class HomeController : Controller

{

private APPCardEntities db = new APPCardEntities();

// GET: Home

public ActionResult Index(int? page, string TransID, string CHName,string CC, string BG, string GL, string WBS, string Order, string accountingPeriod, string SelectedComp )`

{

UserModel model = new UserModel();

int pageSize = 25;

int pageNumber = (page ?? 1);

var startOfthisMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 13);

var firstDay = startOfthisMonth.AddMonths(-13);

var lastDay = startOfthisMonth.AddDays(-13);

var q = (from o in db.AccrualDetailViews

where o.LoadDate >= firstDay

&& o.LoadDate <= lastDay

select o);

var total = q.Count().ToString("N0");

ViewBag.Counts = q.Count().ToString("N0");

ViewBag.Total = (from o in db.AccrualDetailViews

where o.LoadDate >= firstDay

&& o.LoadDate <= lastDay

select o).Sum(o => o.TransAmount).Value.ToString("c");

var PeriodDates = (from o in db.AccrualDetailViews

where o.LoadDate >= firstDay

&& o.LoadDate <= lastDay

orderby o.LoadDate descending

select o)

.ToList()

.Select(o => o.LoadDate.Value.ToShortDateString())

.Distinct();

ViewBag.accountingPeriod = new SelectList(PeriodDates);

var Comps = db.AccrualDetailViews.Select(C=> new SelectListItem {

Value = C.Comp,

Text = C.Comp,

Selected = true,

}).Distinct().ToList();

ViewBag.CompsList = new MultiSelectList(Comps, "Value", "Text",);

var PcardList = (from o in db.AccrualDetailViews

orderby o.LoadDate descending

where o.LoadDate >= firstDay && o.LoadDate <= lastDay

select new UserModel

{

LoadDate = o.LoadDate,

TransID = o.TransID + "-" + o.BatchID,

CHName = o.CHName,

CC = o.CC + "-" + o.CCDesc,

BG = o.BG + "-" + o.BGDesc,

GL = o.GL + "-" + o.ETDesc,

Comp = o.Comp,

TransAmount = o.TransAmount,

WBS = o.WBS,

Ord = o.Ord,

MerchName = o.MerchName,

Purpose = o.Purpose,

TransDate = o.TransDate,

PostDate = o.PostDate

});

if (SelectedComp != null)

{

PcardList = PcardList.Where(x => x.Comp.Equals(SelectedComp));

ViewBag.Total = PcardList.Where(x => x.TransAmount == x.TransAmount).Sum(x => x.TransAmount).Value.ToString("c");

ViewBag.Counts = PcardList.Where(x => x.Comp == x.Comp).Count().ToString("N0");

}

if (!string.IsNullOrEmpty(TransID))

{

PcardList = PcardList.Where(x => x.TransID.ToUpper().Contains(TransID.ToUpper()));

ViewBag.Total = PcardList.Where(x => x.TransAmount == x.TransAmount).Sum(x => x.TransAmount).Value.ToString("c");

ViewBag.Counts = PcardList.Where(x => x.TransID == x.TransID).Count().ToString("N0");

ModelState.Remove("TransID");

}

if (!string.IsNullOrEmpty(CHName))

{

PcardList = PcardList.Where(x => x.CHName.ToUpper().Contains(CHName.ToUpper()));

ViewBag.Total = PcardList.Where(x => x.TransAmount == x.TransAmount).Sum(x => x.TransAmount).Value.ToString("c");

ViewBag.Counts = PcardList.Where(x => x.Comp == x.Comp).Count().ToString("N0");

ModelState.Remove("CHName");

}

if (!string.IsNullOrEmpty(CC))

{

PcardList = PcardList.Where(x => x.CC.ToUpper().Contains(CC.ToUpper()));

ViewBag.Total = PcardList.Where(x => x.TransAmount == x.TransAmount).Sum(x => x.TransAmount).Value.ToString("c");

ViewBag.Counts = PcardList.Where(x => x.Comp == x.Comp).Count().ToString("N0");

ModelState.Remove("CC");

}

if (!string.IsNullOrEmpty(BG))

PcardList = PcardList.Where(x => x.BG.ToUpper().Contains(BG.ToUpper()));

ViewBag.Total = PcardList.Where(x => x.TransAmount == x.TransAmount).Sum(x => x.TransAmount).Value.ToString("c");

ViewBag.Counts = PcardList.Where(x => x.Comp == x.Comp).Count().ToString("N0");

ModelState.Remove("BG");

if (!string.IsNullOrEmpty(GL))

{

PcardList = PcardList.Where(x => x.GL.ToUpper().Contains(GL.ToUpper()));

ViewBag.Total = PcardList.Where(x => x.TransAmount == x.TransAmount).Sum(x => x.TransAmount).Value.ToString("c");

ViewBag.Counts = PcardList.Where(x => x.Comp == x.Comp).Count().ToString("N0");

ModelState.Remove("GL");

}

if (!string.IsNullOrEmpty(WBS))

{

PcardList = PcardList.Where(x => x.WBS.StartsWith(WBS));

ViewBag.Total = PcardList.Where(x => x.TransAmount == x.TransAmount).Sum(x => x.TransAmount).Value.ToString("c");

ViewBag.Counts = PcardList.Where(x => x.WBS == x.WBS).Count().ToString("N0");

ModelState.Remove("WBS");

}

if (!string.IsNullOrEmpty(Order))

{

PcardList = PcardList.Where(x => x.Ord.StartsWith(Order));

ViewBag.Total = PcardList.Where(x => x.TransAmount == x.TransAmount).Sum(x => x.TransAmount).Value.ToString("c");

ViewBag.Counts = PcardList.Where(x => x.Ord == x.Ord).Count().ToString("N0");

ModelState.Remove("Order");

}

if (!string.IsNullOrEmpty(accountingPeriod))

{

DateTime search_date = Convert.ToDateTime(accountingPeriod);

PcardList = PcardList.Where(x => x.LoadDate == search_date);

ViewBag.Total = PcardList.Where(x => x.TransAmount == x.TransAmount).Sum(x => x.TransAmount).Value.ToString("c");

ViewBag.Counts = PcardList.Where(x => x.Comp == x.Comp).Count().ToString("N0");

ModelState.Remove("accountingPeriod");

}

return View(PcardList.ToPagedList(pageNumber, pageSize));

}

}

}`

The first if statement (selectedComp !=null) will only return the first selected.

My view looks like this:

`@model IPagedList<pcard.Models.ViewModel.UserModel>

@using PagedList.Mvc;

@using PagedList;

<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{

ViewBag.Title = "Index";

}

@using (Html.BeginForm("Index", "Home", FormMethod.Get))

{

<div class="row">

<div class="col-md-4">

<div>

Trans ID (Contains):@Html.TextBox("TransID")<br>

Name(contains):@Html.TextBox("CHName")<br>

Cost Center(begins with):@Html.TextBox("CC")<br>

Budget Group(begins with):@Html.TextBox("BG")<br>

Expense Type(contains):@Html.TextBox("GL") <br>

Accounting Period:@Html.DropDownList("accountingPeriod", "All")

</div>

</div>

<div class="col-md-4">

<div>WBS: (begins with)@Html.TextBox("WBS")<br>

Order: (begins with)@Html.TextBox("Order")<br>

Company Code:@Html.ListBox("SelectedComp", (MultiSelectList)ViewBag.CompsList)<br><br>

<input type="submit" name="submit" value="Search" />

</div>

</div>

<div class="col-md-4">

<label>Count:&nbsp;</label>@ViewBag.Counts<br />

<label>Subtotal:&nbsp;</label> @ViewBag.Total

</div>

</div>

}

<div class="row">

<div class="col-md-12">

<table class="table" border="1">

<tr>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().LoadDate)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().TransID)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().CHName)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().CC)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().BG)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().GL)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().Comp)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().TransAmount)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().WBS)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().Ord)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().MerchName)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().Purpose)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().TransDate)</th>

<th>@Html.DisplayNameFor(model => model.FirstOrDefault().PostDate)</th>

</tr>

@foreach (var item in Model)

{

<tr>

<td>@Html.DisplayFor(modelItem => item.LoadDate)</td>

<td>@Html.DisplayFor(modelItem => item.TransID)</td>

<td>@Html.DisplayFor(modelItem => item.CHName)</td>

<td>@Html.DisplayFor(modelItem => item.CC)</td>

<td>@Html.DisplayFor(modelItem => item.BG)</td>

<td>@Html.DisplayFor(modelItem => item.GL)</td>

<td>@Html.DisplayFor(modelItem => item.Comp)</td>

<td>@Html.DisplayFor(modelItem => item.TransAmount)</td>

<td>@Html.DisplayFor(modelItem => item.WBS)</td>

<td>@Html.DisplayFor(modelItem => item.Ord)</td>

<td>@Html.DisplayFor(modelItem => item.MerchName)</td>

<td>@Html.DisplayFor(modelItem => item.Purpose)</td>

<td>@Html.DisplayFor(modelItem => item.TransDate)</td>

<td>@Html.DisplayFor(modelItem => item.PostDate)</td>

</tr>

}

</table>

<br />

page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index", new

{

page,

TransID = Request.QueryString["TransID"],

accountingPeriod = Request.QueryString["accountingPeriod"],

CHName = Request.QueryString["CHName"],

CC = Request.QueryString["CC"],

BG = Request.QueryString["BG"],

GL = Request.QueryString["GL"],

WBS = Request.QueryString["WBS"],

Order = Request.QueryString["Order"],

SelectedComp = Request.QueryString["SelectedComp"]

}))

</div>

</div>`

As you can see I am using pagedlist. Any help would greatly be appreciated. The only thing I can not get to work is filtering on multiple selected values other then the first value selected. Thank you.

分享给朋友:
您可能感兴趣的文章:
随机阅读: