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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. openswan-ipsec.conf配置说明

    Name ipsec.conf - IPsec configuration and connections Description The optional ipsec.conf file speci ...

  5. 【转】How to hire——创业公司应该如何招人

    How to hire After startups raise money, their next biggest problem becomes hiring.  It turns out it’ ...

  6. JavaScript 对象的创建

    Object类型是JavaScript中使用最多的一种类型.创建Object实例的方式有多种,接下来一一列举. 1. Object构造函数 person1的friends属性修改影响了person2的 ...

  7. [转帖]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 ...

  8. protocol

    For every object that can have a delegate, there is a corresponding protocol that declares themessag ...

  9. 【英语魔法俱乐部——读书笔记】 1 初级句型-简单句(Simple Sentences)

    第一部分 1 初级句型-简单句(Simple Sentences):(1.1)基本句型&补语.(1.2)名词短语&冠词.(1.3)动词时态.(1.4)不定式短语.(1.5)动名词.(1 ...

随机推荐

  1. LeetCode 231

    Power of Two Given an integer, write a function to determine if it is a power of two. /************* ...

  2. 关于Linux测试题

    Linux 测试题: 1. 在Linux系统中,以 文件 方式访问设备 . 2. 某文件的权限为:drw-r--r--,用数值形式表示该权限,则该八进制数为: 644 ,该文件属性是 目录 . 3. ...

  3. Acrobat 2015 win32破解版

    Acrobat 2015 win32破解版,带破解补丁dll覆盖即可 百度云盘:http://pan.baidu.com/s/1i4tFnNJ

  4. JavaScript基础笔记一

    一.真假判断 真的:true.非零数字.非空字符串.非空对象 假的:false.数字零.空字符串.空对象.undefined 例: if(0){ alert(1) }else{ alert(2) } ...

  5. Quartz Scheduler(2.2.1) - Working with TriggerListeners and JobListeners

    TriggerListeners and JobListeners Listeners are objects that you create to perform actions based on ...

  6. Redis 命令 - Sorted Sets

    ZADD key score member [score member ...] Add one or more members to a sorted set, or update its scor ...

  7. c++ 11 国标标准方面的异常处理与微软在Visual Studio 2012的异常处理的区别

    这段代码: __try { } __except(GetErrorCode()) { } 可以捕获空指针,但是包围在其中的代码不能有自带析构函数的对象.c++ 11 标准里面的auto_ptr关键字, ...

  8. SQLserver的存储过程

    存储过程 [Create是创建存储过程,alter是更改.改变存储过程] [在第一次写存储过程时用create,若修改存储过程程序之后,则alter替换create再执行] [在数据库中begin   ...

  9. .NET的JSNO 序列化跟反序列化

    由于本人最近在写webservice,之前一直都同通过AJAX,在服务端处理业务,但是最近需要写一些接口给其他人用,需要使用jsno的序列化与反序列化,什么是jsno就不多说,jsno的好处也不多说, ...

  10. streams 日差管理及监控

    第一部分 stream环境的日常管理 1.capture进程管理 --capture进程信息 SET LINESIZE 200 COLUMN CAPTURE_NAME HEADING 'Capture ...