我是Mvc的新手,我不知道如何使用下拉列表(IEnumerable)在mvc中实现搜索?
IEnumerable
@model IEnumerable<PinkPearl.Models.GenerateTicket_Details>
我认为这可以绑定到下拉列表
@using (Html.BeginForm("ReportCostumeWise", "Index", FormMethod.Post, new { }))
{
<div class="col-md-12" style="padding-left: 0px; padding-bottom: 10px; padding-right: 0px;">
<div class="col-md-1" style="padding-left: 0px; width: 6%;">Costume</div>
<div class="col-md-2" style="width: 7%;padding:0PX;">
@Html.DropDownList("CostumeNameValue", new SelectList(ViewBag.MyCustomCollection as System.Collections.IEnumerable, "CostumeName", "CostumeName"),
"Costume", new { @class = "collection", name = "Value", onchange = "CostumeNameFunction()", style = " border-color: #dddddd;width: 113%; padding: 7px; border-radius: 4px;" })
</div>
<div class="col-md-1" style="width: 14%;">
<input type="submit" id="btnsearch" value="Search" class="btn" style="float: left" />
</div>
</div>
}
表列表
<table class="table user-list" style="width: 97%;">
<thead>
<tr style="background-color: #c7e6ef;">
<th style="min-width: 130px; text-align: left; padding-left: 10px"><span>TicketNo</span></th>
<th style="min-width: 130px; text-align: left; padding-left: 10px; display: none;"><span>Name</span></th>
<th style="min-width: 130px; text-align: left; padding-left: 10px;"><span>Bill Date</span></th>
<th style="min-width: 130px; text-align: left; padding-left: 10px"><span>Quantity</span></th>
<th style="min-width: 130px; text-align: left; padding-left: 10px"><span>Bill Amount</span></th>
<th style="min-width: 130px; text-align: left; padding-left: 10px"><span>Refund </span></th>
<th style="min-width: 130px; text-align: left; padding-left: 10px"><span>Rent Amount</span></th>
<th style="min-width: 130px; text-align: left; padding-left: 10px"><span>Costume</span></th>
<th class="text-center" style="min-width: 100px;">SUMMARY</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr @(item.TotalAmount == Convert.ToDecimal(0) ? " class=\ttr-zero" : string.Empty)>
<td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.TicketNo)</td>
<td style="padding-left: 12px; display: none;">@Html.DisplayFor(modelItem => item.name)</td>
<td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.ArrDate)</td>
<td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.quantity)</td>
<td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.Total)</td>
<td>@Html.DisplayFor(modelItem => item.Refund)</td>
<td>@Html.DisplayFor(modelItem => item.TotalAmount)</td>
<td style="padding-left: 12px;">@Html.DisplayFor(modelItem => item.CostumeName)</td>
<td style="width: 20%; padding-left: 12px; text-align: center;">
<a href="javascript:void(0);" class="anchorDetail" data-id="@item.TicketNo" >
<span class="fa-stack">
<i class="fa fa-square fa-stack-2x"></i>
<i class="fa fa-eye fa-stack-1x fa-inverse"></i>
</span>
</a></td>
</tr>
}
</tbody>
<tbody>
<tr>
<td></td>
<td></td>
<td>@ViewBag.Quantity</td>
<td>@ViewBag.Total</td>
<td>@ViewBag.REFUND</td>
<td>@ViewBag.TotalAmount</td>
</tr>
</tbody>
</table>
我在模型中使用了它
public class GenerateTicket_Details
{
public List<SelectListItem> RateTable { get; set; }
public int Id { get; set; }
public string TicketNo { get; set; }
public string name { get; set; }
public string gender { get; set; }
public string Mobile { get; set; }
public string ArrDate { get; set; }
public string ArrTime { get; set; }
public string ArrDateTime { get; set; }
public string quantity { get; set; }
public decimal Total { get; set; }
public decimal Refund { get; set; }
public decimal TotalAmount { get; set; }
public string GenrateBy { get; set; }
public string cancelby { get; set; }
public string CancelRefundRemark { get; set; }
public string canceleddatetime { get; set; }
public string CostumeName { get; set; }
public List<SelectListItem> MasterCostume { get; set; }
public List<SelectListItem> UserMaster { get; set; }
}
控制器
public ActionResult ReportCostumeWise(string GenrateBy, string TicketNo, string hdnfromdate, string hdntoDate, string CostumeName, FormCollection fr)
{
if (Session["UserID"] == null)
{
return RedirectToAction("LogOut");
}
con.Open();
List<UserMaster> user = new List<UserMaster>();
string sql = "select UserId from UserMaster where flag=1 order by ID desc";
SqlCommand cmd1 = new SqlCommand(sql, con);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
cmd1.CommandType = CommandType.Text;
DataSet ds1 = new DataSet();
da1.Fill(ds1);
foreach (DataRow dr in ds1.Tables[0].Rows)
{
user.Add(new UserMaster() { UseId = dr[0].ToString() });
}
cmd1.ExecuteNonQuery();
List<GenerateTicket_Details> bk = new List<GenerateTicket_Details>();
string query = "";
query += @" select TicketNo,GenrateBy,ArrDate,sum(CostumeQuantity) As Quantity, sum(CostumeTotalPrice) As Total, sum(ISNULL(RefundAmount,0)) AS REFUND
,sum(ISNULL(CostumeTotalPrice,0) - ISNULL(RefundAmount,0)) AS TotalAmount, GenrateBy,CostumeName from GenerateTicket where 1=1 ";
if (!String.IsNullOrEmpty(CostumeName))
{
query += " and CostumeName='" + CostumeName + "' ";
}
query += @" Group by GenrateBy,TicketNo,GenrateBy,ArrDate,CostumeName order by TicketNo desc ";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.CommandType = CommandType.Text;
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
bk.Add(new GenerateTicket_Details() { TicketNo = dr[0].ToString(), name = dr[1].ToString(), ArrDate = dr[2].ToString(), quantity = dr[3].ToString(), Total = Convert.ToDecimal(dr[4].ToString()), Refund = Convert.ToDecimal(dr[5].ToString()), TotalAmount = Convert.ToDecimal(dr[6].ToString()), GenrateBy = dr[7].ToString(), CostumeName = dr[8].ToString() });
}
cmd.ExecuteNonQuery();
ViewBag.Quantity = ds.Tables[0].Select().Sum(w => (int)w["Quantity"]);
ViewBag.TotalAmount = ds.Tables[0].Select().Sum(w => (decimal)w["TotalAmount"]);
ViewBag.REFUND = ds.Tables[0].Select().Sum(w => (decimal)w["REFUND"]);
ViewBag.Total = ds.Tables[0].Select().Sum(w => (decimal)w["Total"]);
ViewBag.MyCustomCollection = bk;
ViewBag.UseId = user;
ViewData["hdnnameexcl"] = GenrateBy;
ViewData["TicketNoexcl"] = TicketNo;
ViewData["hdnfromdateexcl"] = hdnfromdate;
ViewData["hdntoDateexcl"] = hdntoDate;
return View(bk);
}
答案 0 :(得分:1)
由于您需要CostumeName
字段/列具有不同的值(非重复值),因此只需使用GroupBy
并使用每个组的第一行创建另一个列表(类似问题here ):
ViewBag.MyCustomCollection = bk.GroupBy(x => x.CostumeName).Select(x => x.FirstOrDefault()).ToList();
另外,通过在传递给视图之前将其投影到SelectList
上,可以简化下拉绑定,因此无需在DropDownList
帮助器中定义列名:
var costumeList = bk.GroupBy(x => x.CostumeName).Select(x => x.FirstOrDefault()).ToList();
ViewBag.MyCustomCollection = new SelectList(costumeList.Select(x => new SelectListItem()
{
Text = x.ToString(),
Value = x.ToString()
}).ToList(), "Value", "Text");
查看
@Html.DropDownList("CostumeNameValue", ViewBag.MyCustomCollection as SelectList, ...)