一、@Html.ActionLink()概述

  在MVC的Rasor视图引擎中,微软采用一种全新的方式来表示从前的超链接方式,它代替了从前的繁杂的超链接标签,让代码看起来更加简洁。通过浏览器依然会解析成传统的a标签。除此之外,还允许我们添加Html属性。下面来看看@Html.ActionLink()的使用方法吧。

二、@Html.ActionLink()的使用详解

  1. @Html.ActionLink("linkText", "actionName")

  这种重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法(Action),默认控制器为当前页面对应的控制器。

  例如,当前页面的控制器为ProductsController:@Html.ActionLink("detial", "Detial")会生成<a href="/Products/Detail">detail</a>

  2. @Html.ActionLink("linkText", "actionName", "controllerName")

  该重载比第一个重载多了一个参数,他指定了控制器的名称。

  例如,@Html.ActionLink("detail", "Detail", "Products")会生成<a href="Products/Detail">detail</a>

  3. @Html.ActionLink("linkText", "actionName", routeValues)

  相对于上一种重载,该重载新增了routeValue参数,routeValue可以向action传递参数。

  例如,@Html.ActionLink("detail", "Detail", new{ id = 1 })会生成<a href="Products/Detail/1">detail</a>

  4. @Html.ActionLink("linkText", "actionName", routeValues, htmlAttributes)

  htmlAttribute可以设置<a>标签的属性。

  例如,@Html.ActionLink("detail", "Detail", new{ id = 1 }, new{ target = "_blank" })会生成<a href="Products/Detail/1" target="_blank">detail</a>,需要注意的是如果写成new{ target="_blank", class="className" }则会报错,因为Class是C#的关键字,此时应该写成@class="className"。

  5. @Html.ActionLink("linkText", "actionName", "controllerName", routeValues, htmlAttributes)

  该种重载汇聚了以上此种重载的所有参数,是功能最全的重载。

三、@Url.Action(),@Html.ActionLink(),Html.RenderAction()和@Html.Action()的区别

1. 返回值不同

  • @Html.Action()返回值为MvcHtmlString。Html.Action比较灵活,可以直接写在页面上,也可以把它赋值给某一变量,处理之后写在页面上。
  • Html.RenderAction()返回值为void。Html.RenderAction必须写在@{}内,直接由Response对象输出。例:@{ Html.RenderAction("Index", "Import");}
  • @Html.ActionLink()返回值也是MvcHtmlString。
  • @Url.Action()返回值为string。MvcHtmlString本质上也是string,两者都可以直接现在页面上。

2. 生成的东西不同

  • @Html.ActionLink("myLink", "CCC", "Import")生成<a href="/Import/CCC">myLink</a>
  • @Url.Action("CCC", "Import")直接生成字符串"/Import/CCC",没有任何tag标签。

 3.Html.RouteLink 与 Url.RouteUrl

两者都是可以指定由哪一个路由来生成Url,其它与上面的ActionLInk,Action一样

 4.Url.Content

将虚拟(相对)路径转换为应用程序绝对路径。

HtmlHelper用法大全(上)

https://blog.csdn.net/qq_33857502/article/details/79648610

HtmlHelper用法大全(下)

https://blog.csdn.net/qq_33857502/article/details/79648666

ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 HtmlHelper-超链接方法的更多相关文章

  1. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 视图模板页

    https://www.cnblogs.com/xlhblogs/archive/2013/06/09/3129449.html MVC Razor模板引擎 @RenderBody.@RenderPa ...

  2. ASP.NET MVC Razor视图引擎攻略

    --引子 看下面一段MVC 2.0的代码. <%if (Model != null){%> <p><%=Model%></p><%}%>&l ...

  3. ASP.NET MVC——Razor视图引擎

    Razor是MVC框架视图引擎,我们今天就来说一说Razor视图引擎. 首先还是来创建一个基础项目叫Razor来演示. 先来定义一个Model叫Product public class Product ...

  4. MVC Razor视图引擎

    Razor 不是编程语言.它是服务器端标记语言. Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法 当网页被写入浏览器时,基于服务器的代码能够创建动 ...

  5. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 动态数据的呈现

    https://www.cnblogs.com/cynchanpin/p/7065098.html 在MVC3開始.视图数据能够通过ViewBag属性訪问.在MVC2中则是使用ViewData.MVC ...

  6. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 Areas区域说明

    https://www.cnblogs.com/webapi/p/5976642.html Asp.Net MVC Areas区域说明   一般网站分为前台+会员后台+管理员后台,做过webform的 ...

  7. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 视图引用资源

    https://www.cnblogs.com/Roxlin/p/5615144.html 一.配置BundleConfig.cs文件 1.首先要在App_Start 里面BundleConfig.c ...

  8. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2

    https://www.bbsmax.com/A/gAJG67OXzZ/ 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用WebForm时代沿留下来的ASP ...

  9. Asp.Net MVC Razor视图引擎与My97DatePicker插件的结合

    using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System. ...

随机推荐

  1. Somatic hypermutation (or SHM) is a cellular mechanism by which the immune system adapts to the new foreign elements that confront it (e.g. microbes), as seen during class switching. Somatic hypermut

    Somatic hypermutation (or SHM) is a cellular mechanism by which the immune system adapts to the new ...

  2. SurfaceView双缓冲技术引入

    package com.loaderman.customviewdemo; import android.content.Context; import android.graphics.Canvas ...

  3. java-mybaits-012-mybatis-Interceptor-拦截器读写分离四种实现方案

    一.概述 基本项目搭建 技术框架:spring web mvc .日志[slf4j.log4j2].mybatis.druid.jetty插件启动.mybatis-generator逆向配置生产dao ...

  4. java 虚拟机类加载 及内存结构

    http://www.jb51.net/article/105920.htm https://www.cnblogs.com/Qian123/p/5707562.html Java类加载全过程 一个j ...

  5. Qt编写自定义控件37-发光按钮(会呼吸的痛)

    一.前言 这个控件是好早以前写的,已经授权过好几个人开源过此控件代码,比如红磨坊小胖,此控件并不是来源于真实需求,而仅仅是突发奇想,类似于星星的闪烁,越到边缘越来越淡,定时器动态改变边缘发光的亮度,产 ...

  6. PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)

    1048 Find Coins (25 分)   Eva loves to collect coins from all over the universe, including some other ...

  7. PAT 甲级 1031 Hello World for U (20 分)(一开始没看懂题意)

    1031 Hello World for U (20 分)   Given any string of N (≥) characters, you are asked to form the char ...

  8. LeetCode_112. Path Sum

    112. Path Sum Easy Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  9. (九)UML之活动图

    一.概念 二. 在Rational rose 中画活动图 2.1 创建Activity Diagram 2.2 画图

  10. 【Leetcode_easy】733. Flood Fill

    problem 733. Flood Fill 题意:图像处理中的泛洪填充算法,常见的有四邻域像素填充法.八邻域像素填充法.基于扫描线的像素填充法,实现方法分为递归与非递归(基于栈). 泛洪填充算法原 ...