MVC中的Html.ActionLink

以下使用参数文字说明:

  1. linkText:生成的链接所显示的文字         类型:string
  2. actionName:对应控制器的方法          类型:string
  3. routeValues:向对应的action传递的参数      类型:object 或 RouteValueDictionary
  4. controlName:指定控制器的名称           类型:string
  5. htmlAttributes:设置<a>标签的属性                   类型:object 或 IDictionary
  6. protocol:指定访问协议如:http等         类型:string
  7. hostName:指定访问域名             类型:string
  8. fragment:指定访问锚点              类型:string

重载一:Html.ActionLink("linkText","actionName")【默认当前页面控制器】

调用:@Html.ActionLink("默认当前页面控制器", "About")

生成效果:<a href="/Home/About">默认当前页面控制器</a>

重载二:Html.ActionLink("linkText","actionName",routeValues)

调用:

  routeValues Is object:

  @Html.ActionLink("默认当前页面控制器", "About", new { id = 1, type = "Dic" })

  routeValues Is RouteValueDictionary:

  @RouteValueDictionary Dictionary = new RouteValueDictionary();
  @Dictionary["id"] = 1;
  @Dictionary["type"] = "Dic";
  

  @Html.ActionLink("默认当前页面控制器", "About", Dictionary)

生成效果:<a href="/Home/About?classid=1">默认当前页面控制器</a>

重载三:Html.ActionLink("linkText","actionName","controlName")

调用:@Html.ActionLink("默认当前页面控制器", "About", "Home")

生成效果:<a href="/Home/About">默认当前页面控制器</a>

重载四:Html.ActionLink("linkText","actionName",routeValues,htmlAttributes)

调用:

  htmlAttributes Is object:

  @Html.ActionLink("首页", "Index", "Home", null, new { @class = "active", target = "_blank" })%>【注:由于class是保留关键字,所以一定要写成@class】

  htmlAttributes Is IDictionary:

  @IDictionary<string, object> AttrDictionary = new Dictionary<string, object>();

  @AttrDictionary["class"] = "active";

  @AttrDictionary["target"] = "_blank";

生成效果:<a class="active" href="/" target="_blank">首页</a>

重载五:Html.ActionLink("linkText","actionName","controlName","protocol","hostName","fragment",routeValues,htmlAttributes)

调用:@Html.ActionLink("关于我们", "About", "Home", "http", "localhost", "top", null, null)

生成效果:<a href="http://localhost:12120/Home/About#top">关于我们</a>

时间: 2024-08-02 22:09:11

MVC中的Html.ActionLink的相关文章

MVC中使用MVCPager简单分页

一.建立数据库以及建立MVC项目 自己随便建立一个数据库,并且添加数据.我建立的数据库如下. 二.建立LINQ to SQL映射. 然后一步步点确定 三.编写代码 在Controllers中建立控制器,模板为空.建立的项目如下: 要添加视图Index 在Controller中选中Index 并且添加视图, 模板选中List, 如下 注意后台要添加对MvcPager的程序集引用,后台代码如下: using Webdiyer.WebControls.Mvc; public class Student

在 asp.net mvc中的简单分页算法

//第一步:建立如下分页实体类:namespace MVCPager.Helpers { /// <summary> /// 简单分页算法类 /// </summary> public class Pager { public int RecordCount { get; set; } public int PageIndex { get; set; } public int PageSize { get; set; } public int PageCount { get { r

6.在MVC中使用泛型仓储模式和依赖注入实现增删查改

原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pattern-and-dep/ 系列目录: Relationship in Entity Framework Using Code First Approach With Fluent API[[使用EF Code-First方式和Fluent API来探讨EF中的关系]] Code First Mig

MVC中HtmlHelper用法大全参考

解析MVC中HtmlHelper控件7个大类中各个控件的主要使用方法(1) 2012-02-27 16:25 HtmlHelper类在命令System.Web.Mvc.Html之中,主要由7个静态类组成,它们分别是FormExtensions类,InputExtensions类,LinkExtensions类,SelectExtensions类,TextExtensions类,ValidationExtensions类,RenderPartialExtensions类. 为了方便开发者使用Htm

MVC中的@section

在前文<MVC中 _ViewStart _Layout Index三个页面中的加载顺序> 中另外指定母版页 Layout.cshtml时...遇到了这个问题.. 报错: 以下各节已定义,但尚未为布局页“~/Views/Shared/Layout2.cshtml”呈现:“BreadCrumb; TailJS”. 后来发现在Index.cshtml中..出现了这些片段 @section BreadCrumb { <ul class="breadcrumb"> <

谈谈asp.net MVC中的AppendTrailingSlash以及LowercaseUrls ,你还记得吗?

asp.net MVC是一个具有极大扩展性的框架,可以在从Url请求开始直到最终的html的渲染之间进行扩展,所以要学好还是需要了解框架的运行原理,推荐Artech. 今天我们回忆的不是MVC中的filter,也不是Controller的激活或者是Action的执行,或者是Url路由RouteData的生成,我们来回忆的是RouteTable.Routes  ,即全局路由表的两个属性.AppendTrailingSlash以及LowercaseUrls. AppendTrailingSlash的

Mvc中ViewBag、ViewData、TempData如何使用

对于我初学者来说,Mvc有很多都是我所迷惑的,也是我了解后所痴迷的.废话不多说,来谈谈我今天对Mvc这几个常用的对象的理解吧,这里面只简明概要叙述 ViewBag 获取动态视图数据字典 作用:给视图传递数据,不需要转换类型,由系统动态解析,比ViewData执行性能要差 ViewData   获取或设置视图数据的字典         给视图传递数据,需要转换成相应的类型,编写没有ViewBag方便,可读性强 TempData     临时数据的字典   给控制器或视图传递数据,需要和ViewDa

在 Asp.NET MVC 中使用 SignalR 实现推送功能 [转]

在 Asp.NET MVC 中使用 SignalR 实现推送功能 罗朝辉 ( http://blog.csdn.net/kesalin ) CC许可,转载请注明出处 一,简介 Signal 是微软支持的一个运行在 Dot NET 平台上的 html websocket 框架.它出现的主要目的是实现服务器主动推送(Push)消息到客户端页面,这样客户端就不必重新发送请求或使用轮询技术来获取消息. 可访问其官方网站:https://github.com/SignalR/ 获取更多资讯. 二,实现机制

mvc中动态给一个Model类的属性设置验证

原文:mvc中动态给一个Model类的属性设置验证 在mvc中有自带的验证机制,比如如果某个字段的类型是数字或者日期,那么用户在输入汉字或者英文字符时,那么编译器会自动验证并提示用户格式不正确,不过这样的验证毕竟功能有限,那么就需要我们自己进行定制验证. 假设有Model类:class Dinners{ private string Title; private System.DateTime EventDate; private string Description; private stri