ASP.NET MVC 学习笔记(三),排序加查找
2024-09-11 16:20:43
首先先说排序
非常的简单
代码如下
//创建视图
public ViewResult Index()
{
//升序排列
IEnumerable<Product> Prodcuts = repository.Products.OrderBy(x => x.Price);
//下面的是降序排列
repository.Products.OrderByDescending(x => x.Price);
List<Product> p = Prodcuts.ToList();
QuickSort(, p.Count - , p);
string s1 = "";
//List<Product> list= Prodcuts.AsQueryable().OrderBy(x => x.Price).ToList();
// foreach (var item in Prodcuts)
// {
// string p = item.Category;
// }
Prodcuts = p;
return View(Prodcuts);
}
核心语句 orderBy 升序,降序 OrderByDescending
查找数据
1 创建查找的动作方法
[HttpPost]
public ActionResult Query(string Name)
{
IEnumerable<Product> p = repository.Products.Where(x => x.Category== Name||x.ProductName.Contains(Name));
if (p != null)
{
return View("QueryResult", p);
}
else
{
return View("QueryResult",p);
} }
没什么的可解释的,
Html 代码
@using Domain.Entities
@model IEnumerable<Product>
@{
ViewBag.Title = "Index";
} <h2>Index</h2>
@* 查询再这里 *@
@using (Html.BeginForm("Query","Product"))
{
@Html.TextBox("Name");
<input type="submit" value="查询" />
}
<table class="table table-bordered table-striped table-condensed" >
<tr>
<th class="text-center">id</th>
<th class="text-center">产品名</th>
<th class="text-center">描述</th>
<th class="text-center">种类</th>
<th class="text-center">价格</th>
<th class="text-center">删除</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.ProductID</td>
<td>@Html.ActionLink(item.ProductName, "Edit", new { item.ProductID })</td>
<td>@item.Description</td>
<td>@item.Category</td>
<td>@item.Price.ToString()</td>
<td>
@using (Html.BeginForm("Delete", "Product"))
{
@Html.Hidden("ProductId", item.ProductID)
<input class="btn btn-danger" type="submit" value="删除"/>
}
</td>
</tr> } </table>
<div class="panel-footer">
@Html.ActionLink("添加", "Create", new { @class="btn btn-default"})
</div>
查询结构视图没什么好解释的就一个Foreach
@using Domain.Entities
@model IEnumerable<Product>
@{
ViewBag.Title = "QueryResult";
} <h2>QueryResult</h2> <table border=""> @if (Model.Count()>)
{
foreach (var item in Model)
{
<tr>
<td>@item.ProductName</td>
<td>@item.Description</td>
<td>@item.Category</td>
<td>@item.Price</td>
</tr>
}
}
else
{
<tr>
<td>没有查询到</td>
</tr>
}
</table>
测试结果
最新文章
- 【Asp.Net Core】二、添加控制器和视图
- android editText 监听事件
- 每天一个linux命令(19):find 命令概览
- Oracle DB 存储增强
- Scala 深入浅出实战经典 第53讲:Scala中结构类型实战详解
- 2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂
- C_functions
- hdu2159二维费用背包
- 网络请求 ---iOS
- JavaWeb(七)之详解JavaWeb路径
- Integration Services 服务连接失败,拒绝访问以及无法检索数据报错问题
- ES6常用
- CarbonData-2:core
- Centos 中 service iptables stop 失败
- VC 中引用js文件
- python数据相关性分析 (计算相关系数)
- 【CLR】详解CLR中的程序集
- LeetCode--455--分发饼干
- zookeeper在Dubbo中扮演了一个什么角色
- iOS 11开发教程(十)iOS11无线连接手机真机测试
热门文章
- VMware安装Windows注意
- SQL时间格式化 转载备用~
- [leetcode]415. Add Strings字符串相加
- iOS.Dev.Support.MultiVersions
- 数据分页c#
- java网络爬虫实现信息的抓取
- 2018.09.09 bzoj4403: 序列统计(Lucas定理)
- 2018.08.28 洛谷P3803 【模板】多项式乘法(FFT)
- MAC安装远程工具Securecrt的破解方式(详细有图)
- UVa 1572 Self-Assembly (构造+拓扑排序。。。。。)