Insist
1、怎么自动截断文本?
如题,当数据库中的数据内容超出了要显示的长度时,如果不采取措施,会破坏页面的布局美观,所以可以采用自动截断文本,需要查看的时候再把其他的内容显示出来。
没截断的时候如下图:
再视图中添加如下代码便可实现截断功能:
@helper Truncate(string input, int length)
{
if (input.Length <= length)
{
@input
}
else
{
@input.Substring(, length)<text>...</text>
}
}
<td>
@Truncate(item.Title, 25)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
看到后面的省略号没有,那就是效果:
2、MVC怎么分页?
当要把数据库中的数据以分页的形式显示出来,可以用到一个分页插件PagedList,可以在VS2013中MSDN论坛搜索下载即可。
首先在bin文件夹下右键点击管理NuGet程序包,搜索并引用程序集:
然后在控制器Index动作中添加如下代码:
public ActionResult Index(int page = )
{
const int PageSize = ;
Models.MvcBookStoreEntities1 db = new Models.MvcBookStoreEntities1();
var Iuser = db.Books.OrderBy(p => p.BookId).ToPagedList(page, PageSize);
return View(Iuser);
}
在相应视图中添加如下代码,便可以实现分页了:
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { page }), new PagedListRenderOptions { LinkToPreviousPageFormat = "上一页", LinkToNextPageFormat = "下一页", MaximumPageNumbersToDisplay = })
效果如下图所示:
3、怎么使用Linq中的Take和ToList方法
首先在Index动作中添加如下代码:
Models.MvcBookStoreEntities1 db = new Models.MvcBookStoreEntities1();
var data = db.Categories.Take().ToList();
return View(data);
然后在添加视图,把原视图调整一下:
<ol>
@foreach (var item in Model)
{
<li>
<h5>
@Html.ActionLink(@item.Name, "Browse", new { id = @item.CategoryId })
</h5>
</li>
}
</ol>
运行结果如下图:
4、怎么使用@Html.ActionLink()和@Url.Action():
@Html.ActionLink()可以有3个参数,比如:
@Html.ActionLink(@item.Name, "Browse", new { id=@item.CategoryId})
第一个参数是要显示出来的字段,第二个是控制器中动作方法名,第三个是要传递的参数
@Url.Action()可以有两个参数,比如:
<a href="@Url.Action("Details",new {id=book.BookId})">
第一个参数动作名,第二个是要传递到Details中的参数。
电放费
Insist的更多相关文章
- If you insist running as root, then set the environment variable RUN_AS_USER=root...
版权声明:本文为博主原创文章,不经博主允许注明链接即可转载. If you insist running as root, then set theenvironment variable RUN_A ...
- 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...
- Lesson 22 A glass envolops
Text My daughter, Jane, never dreamed of receiving a letter from a girl of her own age in Holland. L ...
- openswan-ipsec.conf配置说明
Name ipsec.conf - IPsec configuration and connections Description The optional ipsec.conf file speci ...
- 【转】How to hire——创业公司应该如何招人
How to hire After startups raise money, their next biggest problem becomes hiring. It turns out it’ ...
- JavaScript 对象的创建
Object类型是JavaScript中使用最多的一种类型.创建Object实例的方式有多种,接下来一一列举. 1. Object构造函数 person1的friends属性修改影响了person2的 ...
- [转帖]The Lambda Calculus for Absolute Dummies (like myself)
Monday, May 7, 2012 The Lambda Calculus for Absolute Dummies (like myself) If there is one highly ...
- protocol
For every object that can have a delegate, there is a corresponding protocol that declares themessag ...
- 【英语魔法俱乐部——读书笔记】 1 初级句型-简单句(Simple Sentences)
第一部分 1 初级句型-简单句(Simple Sentences):(1.1)基本句型&补语.(1.2)名词短语&冠词.(1.3)动词时态.(1.4)不定式短语.(1.5)动名词.(1 ...
随机推荐
- Mac 10.9 自带apache2虚拟目录设置
花了好几天时间做这个事,终于成功,把正确的做法记录一下. 如果是第一次使用apache,可以先执行sudo apachectl start,然后在浏览器里打开http://localhost看看效果, ...
- 转:Linux网络IO并行化技术概览
转:http://codinginet.com/articles/view/201605-linux_net_parallel?simple=1&from=timeline&isapp ...
- python中那些双下划线开头得函数和变量--转载
Python中下划线---完全解读 Python 用下划线作为变量前缀和后缀指定特殊变量 _xxx 不能用'from module import *'导入 __xxx__ 系统定义名字 __x ...
- asp.net 文件压缩zip下载
今天分享下昨天做的一个东西 asp.net 的文件 zip 批量下载,首先你需要去 到http://dotnetzip.codeplex.com这个站点下载zip 的包,在里面找到 Ionic.Z ...
- [算法练习] UVA 10420 - List of Conquests?
UVA Online Judge 题目10420 - List of Conquests 问题描述: 题目很简单,给出一个出席宴会的人员列表,包括国籍和姓名(姓名完全没用).统计每个国家有多少人参加, ...
- Redis 命令 - Connection
AUTH password Authenticate to the server More: http://redis.io/commands/auth, http://www.redis.cn/co ...
- Nginx - Additional Modules, Limits and Restrictions
The following modules allow you to regulate access to the documents of your websites — require users ...
- 【SQLServer】使用T-SQL访问远程数据库:openrowset 和 openquery 以及连接服务器的创建
█ 启用/关闭Ad Hoc Distributed QueriesAd Hoc Distributed Queries服务默认是关闭的,要使用openrowset 和 openquery访问远程数据库 ...
- WinForm 实现登录,验证成功,关闭登录界面,显示主界面
点击登录按钮时: ") { this.DialogResult = DialogResult.OK; this.Close(); } else { MessageBox.Show(" ...
- 20141015--for语句1
for 语句 break (跳出循环体) 100节楼梯,第1-49节分数等于节数,50以后(包括50)每节10分,