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 ...
随机推荐
- 使用copy再次实现Circle类,保证不能有内存泄漏问题
#import <Foundation/Foundation.h> //xieyi @protocol showOn @required -(void)printOn; @end // l ...
- AngularJs学习经验汇集
>>关于ng-include 有时候你会发现你用这个指令想要加载某个模板总是加载不出来,url明明是对的,页面还是一片空白,这里有一个细节要注意以下: <div ng-include ...
- oracle 学习笔记 复制表结构
1.复制表结构以及数据 create table d_table_name as select * from s_table_name; ---注意并不会创建索引 2.只复制表结构 create t ...
- Jquery Easyui验证扩展,Easyui验证,Easyui校验,js正则表达式
Jquery Easyui验证扩展,Easyui验证,Easyui校验,js正则表达式 >>>>>>>>>>>>>> ...
- backboneJs 导图
- Jersey(1.19.1) - Representations and Java Types
Previous sections on @Produces and @Consumes referred to MIME media types of representations and sho ...
- Delphi 获取进程路径及命令行参数
Delphi 获取进程路径及命令行参数, 但有的进程获取时会报错,不知为啥 type PVOID64 = UINT64; _UNICODE_STRING = packed record Length ...
- Apache httpd开启SSL
https://my.oschina.net/xpbug/blog/197454 必要软件 apache httpd 2.4(windows) perl 5 (windows) openssl (wi ...
- 在ASP.NET项目中使用CKEditor +CKFinder实现图片上传功能
前言 之前的项目中一直使用的是FCKeditor,昨天突然有个想法:为什么不试一下新的CKEditor呢?于是花了大半天的时间去学习它的用法,现在把我的学习过程与大家分享一下. 谈起FCKeditor ...
- 非Page类使用session(Httpcontext.session和page.session区别)
ASP.NET中Session高级使用技巧 在开发Aspx .NET软件时,有时需要把常用的东西封装到一个非PAGE类中,文章介绍在非Page类中使用Session的方法. 一.PAGE参数法: 1. ...