使用MVC中的下拉列表过滤WebGrid中的记录 [英] Filtering records in WebGrid using dropdownlist in MVC
问题描述
使用MVC中的下拉列表过滤WebGrid中的记录
conroller:
[HttpPost]
公共ActionResult搜索(FormCollection formCollection)
{
DataClasses1DataContext objentity = new DataClasses1DataContext();
var students = new List();
var filterBy = formCollection.Get(name);
var filterText = formCollection.Get(SearchKey);
开关(filterBy)
{
casename :
students = objentity.Employees.Where(p => p.name.Contains(filterText))。ToList();
break;
}
ViewBag.name = new SelectList(objentity.Employees.ToList(),ParameterName,ParameterName);
返回视图(索引,Pro ductList);
}
[HttpPost]
公共ActionResult搜索(string filterText)
{
var students = new DataClasses1DataContext()。Employees.Where(p =>
p.name.Contains(filterText)
)。ToList();
返回PartialView(_ employeelist,学生);
}
查看
@model MvcApplication1.Models.ProductDetailModel
@ {
ViewBag.Title =搜索;
}
< style type =text / css>
表,td,th
{
边框:1px稳定绿色;
border-collapse:collapse;
}
th
{
边框:1px纯黑色;
背景颜色:绿色;
颜色:白色;
}
< / style>
< script language =javascript>
function AlertMessage( ){
alert(这是一条警报信息);
}
< / script> ;
@using(@ Html.BeginForm(搜索,首页))
{
@ Html.DropDownList(name) 按详细搜索:@ Html.TextBox(SearchKey,) | < input type =submitvalue = 搜索onclick =filter()/> |
@ Html.Partial(_ employeelist,型号) |
var grid = new WebGrid(Model .ProductList,canSort:false);
@ grid.GetHtml(列:
grid.Columns
(
grid.Column(id,id),
grid.Column(name,名称),
grid.Column(书籍,书籍)
),模式:Web GridPagerModes.Numeric)
}
@ Html.ActionLink(导出到Excel,ExportExcel)
@ Html.ActionLink(导出为PDF, ExportPDF)
@ Html.ActionLink(返回列表,查看书)
@using(Html.BeginForm(ExportData,Home,FormMethod.Post,new {enctype =multipart / form-data}))
{
< input type =submitname =Exportid =Exportvalue =Export/>
}
型号:
public class ProductDetailModel
{
public List< Product> ProductList { get ; set ; }
}
public class 产品
{
public int id {获得跨度>; set ; }
public string name { get 跨度>; set ; }
public string books { get 跨度>; set ; }
}
.ajax({
type:POST
,url:主页/搜索
,数据:JSON.stringify({filterText:
(#SearchKey)。val ()})
,contentType:'application / json;'
,成功:函数(结果){
< blockquote>(#tblStudents)。html(结果);
}
});
}
< / script>
var grid = new WebGrid(Model.ProductList,canSort: false);
@ grid.GetHtml(栏目:
grid.Columns
(
grid.Column(id,id),
grid.Column(name,name),
grid.Column(书籍,书籍)
),模式:WebGridPagerModes.Numeric)
}
@ Html.ActionLink(导出到Excel, ExportExcel)
@ Html.ActionLink(导出为PDF,ExportPDF)
@ Html.ActionLink(返回列表,查看书)
@using(Html.BeginForm(ExportData,Home,FormMethod.Post,new {enctype =multipart / form-data}))
{
< input type =submitname =Exportid =Exportvalue =Export />
}
型号:
public class ProductDetailModel
{
public List< Product> ProductList { get ; set ; }
}
public class 产品
{
public int id {获得跨度>; set ; }
public string name { get 跨度>; set ; }
public string books { get 跨度>; set ; }
}
Filtering records in WebGrid using dropdownlist in MVC
conroller:
[HttpPost]
public ActionResult search(FormCollection formCollection)
{
DataClasses1DataContext objentity = new DataClasses1DataContext();
var students = new List();
var filterBy = formCollection.Get("name");
var filterText = formCollection.Get("SearchKey");
switch (filterBy)
{
case "name":
students = objentity.Employees.Where(p => p.name.Contains(filterText)).ToList();
break;
}
ViewBag.name = new SelectList(objentity.Employees.ToList(), "ParameterName", "ParameterName");
return View("Index", ProductList);
}
[HttpPost]
public ActionResult search(string filterText)
{
var students = new DataClasses1DataContext().Employees.Where(p =>
p.name.Contains(filterText)
).ToList();
return PartialView("_employeelist", students);
}
view
@model MvcApplication1.Models.ProductDetailModel
@{
ViewBag.Title = "Search";
}
<style type="text/css">
table, td, th
{
border: 1px solid green;
border-collapse: collapse;
}
th
{
border: 1px solid black;
background-color: green;
color: white;
}
</style>
<script language="javascript">
function AlertMessage() {
alert("This is a alert message");
}
</script>
@using (@Html.BeginForm("Search", "Home"))
{
@Html.DropDownList("name") Search by detail: @Html.TextBox("SearchKey", "") | <input type="submit" value="Search" onclick="filter()" /> |
@Html.Partial("_employeelist", Model)
|
var grid = new WebGrid(Model.ProductList, canSort: false);
@grid.GetHtml(columns:
grid.Columns
(
grid.Column("id", "id"),
grid.Column("name", "name"),
grid.Column("books", "books")
), mode: WebGridPagerModes.Numeric)
}
@Html.ActionLink("Export to Excel", "ExportExcel")
@Html.ActionLink("Export to PDF", "ExportPDF")
@Html.ActionLink("Back to List", "viewbooks")
@using (Html.BeginForm("ExportData", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="submit" name="Export" id="Export" value="Export"/>
}
Model:
public class ProductDetailModel
{
public List<Product> ProductList { get; set; }
}
public class Product
{
public int id { get; set; }
public string name { get; set; }
public string books { get; set; }
}
.ajax({
type: "POST"
, url: "Home/search"
, data: JSON.stringify({ filterText:
("#SearchKey").val() })
, contentType: 'application/json;'
, success: function (result) {
("#tblStudents").html(result);
}
});
}
</script>
var grid = new WebGrid(Model.ProductList, canSort: false);
@grid.GetHtml(columns:
grid.Columns
(
grid.Column("id", "id"),
grid.Column("name", "name"),
grid.Column("books", "books")
), mode: WebGridPagerModes.Numeric)
}
@Html.ActionLink("Export to Excel", "ExportExcel")
@Html.ActionLink("Export to PDF", "ExportPDF")
@Html.ActionLink("Back to List", "viewbooks")
@using (Html.BeginForm("ExportData", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="submit" name="Export" id="Export" value="Export"/>
}
Model:
public class ProductDetailModel { public List<Product> ProductList { get; set; } } public class Product { public int id { get; set; } public string name { get; set; } public string books { get; set; } }
这篇关于使用MVC中的下拉列表过滤WebGrid中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!