`
lovnet
  • 浏览: 6704804 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Learn ASP.NET MVC (第五回)理论篇

阅读更多

通过前几篇文章,我们通过ASP.NET MVC创建了一个简单的应用程序,学习了ASP.NET MVC的基本框架和工作流程。但是Demo中很多东西都是VS为我们自动生成的,比如View的html代码等。此文和大家一起学习一下ASP.NET MVC 中的Helper (辅助方法) --> HtmlHelperUrlHelper
何谓Helper,其实就是在View中为了实现一些灵活功能而写的方法组,主要的目的就是实现将界面与逻辑分离。

HtmlHelper

ASP.NET MVC提供了一个HtmlHelper用于生成Html标签和有数据绑定的Html标签。
1.ActionLink
其中最常用的就是Html.ActionLink。
ActionLink中的三个参数分别为:显示的文字,Action ,Controller 。其中Controller可以省略,省略时指向同一Controller下的Action。
---基本应用:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><%=Html.ActionLink("Hello","Index","Home")%>

在UrlRouting规则为默认规则的情况下,它生成的HTML代码为:<a href="/">Hello</a>
---带有QueryString的写法:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><%=Html.ActionLink("Hello","Index","Home",new{page=1},null)%>

-------<a href="/?page=1">Hello</a>
如果连接中不涉及到action及controller就没有必要使用ActionLink,而是直接写HTML代码就可以了。<a href="#">Hello</a>
2.Html表单元素
(1) Form:
我们当然可以使用纯的Html代码:
<form action="/home/index" method="post"></form>
但是因为是在HTML的属性中,所以还是难以维护,ASP.NET MVC为我们提供了一个Helper,我们可以通过以下两种方式生成一个Form:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><%using(Html.BeginForm("index","home",FormMethod.Post)){%>表单内容
<%}%>

<%Html.BeginForm("index","home",FormMethod.Post);%>表单内容
<%Html.EndForm();%>


(2) 其他html标签:
ASP.NET MVC提供了多种表单元素的Helper。其中包括:TextBox(类似input type=text)、TextArea、DropDownList(select)、CheckBoxHidden、ListBox、Password、RadionButton。

如果我们想提供一个input type=text 它的name为t1则以下代码:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><%=Html.TextBox("t1")%>


(3) 表单元素绑定
如果我们想要让上文中的t1初始时就有一个值:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><%=Html.TextBox("t1","Hello")%>

如果数据是从数据库中读取,即得到数据是从Action中获取的,那么我们可以在Action中使用ViewData传递。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->Action:
ViewData["text"]="Hello";
View:
<%=Html.TextBox("t1",ViewData["text"])%>


UrlHelper

1.UrlHelper.Action
UrlHelper的Action方法 用于生成一个URL地址:
Controller下Index:<%=Url.Action(“Index”)%> --------index
Controller下Index:<%=Url.Action(“Index”,new{id=1})%> --------index?id=1
UsersController下Index:<%=Url.Action(“Index”,"Users")%> --------/Users/index
UsersController下Index: <%=Url.Action(“Index”,"Users",new{id=1})%> /Users/index?id=1
在View中写Url.Action("Index","Home"),运行后则会生成/Home/Index这个地址。

2.UrlHelper.Encode
这也是UrlHelper的一个方法 使用方法 如<%=Url.Encode("中文")%>,功能与Server.UrlEncode相同。

3.UrlHelper.Content
用于服务器路径转换为绝对路径。如:
<%=Url.Content("~/content/site.css") %>最后的结果为:/content/site.css

分享到:
评论

相关推荐

    Pro ASP.NET MVC 5 epub

    The ASP.NET MVC 5 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven ...

    Programming Microsoft ASP.NET MVC, 3rd Edition

    Go deep into the architecture and features of ASP.NET MVC 5, and learn how to build web applications that work well on both the desktop and mobile devices. Web development expert Dino Esposito takes ...

    Bootstrap for ASP.NET MVC

    The book includes practical examples to show you how to use open source plugins with Bootstrap and ASP.NET MVC and will guide you through building an ASP.NET MVC website using Bootstrap, utilizing ...

    Bootstrap.for.ASP.NET.MVC.2nd.Edition.17858894

    Create a new ASP.Net MVC 6 project that uses Bootstrap for its styling and learn how to include external libraries using the new package managers Learn to use the various Bootstrap CSS and ...

    Test-Drive ASP.NET MVC

    You’ll learn how to do everything from creating your first test, to building REST web services, to deploying your finished ASP.NET MVC applications. Test-Drive ASP.NET MVC gets you started with all ...

    ASP.NET MVC with Entity Framework and CSS

    People wanting to learn ASP.NET MVC and Entity Framework ranging from novice developers new to the subject through to more experienced ASP.NET web forms developers looking to migrate from web forms to...

    Bootstrap for ASP.NET MVC, 2nd Edition

    Create a new ASP.Net MVC 6 project that uses Bootstrap for its styling and learn how to include external libraries using the new package managers Learn to use the various Bootstrap CSS and ...

    Learning ASP.NET Core MVC Programming

    "Learning ASP.NET Core MVC Programming" English | ISBN: 1786463830 | 2016 | EPUB | 342 pages | 17 MB Key Features Get a first-principles coverage of ASP.NET MVC and the latest release, Core This book...

    Pro ASP.NET MVC 5.mobi

    The ASP.NET MVC 5 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven ...

    Pro ASP.NET MVC 2 Framework, Second Edition

    Gain a solid architectural background to ASP.NET MVC V2, including MVC and REST concepts. Explore the entire ASP.NET MVC Framework and take a detailed look at the official MVC development toolkit. ...

    Pro ASP.NET MVC 5.pdf

    The ASP.NET MVC 5 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven ...

    无涯教程(LearnFk)-ASP.Net-MVC教程离线版.pdf

    无涯教程网(learnfk)整理提供:ASP.NET MVC框架是微软在ASP.NET中所添加的一组MVC类别库。

    ASP.NET MVC Framework Unleashed(Stephen Walther)

    Through this case study, you'll learn how to build ASP.NET MVC applications using test-driven development processes that enable rapid feedback, greater productivity, and better quality. Throughout,...

    ASP.NET MVC 2 in action

    In ASP.NET MVC 2 in Action, readers learn how to move from web form-based development to designs based on the MVC pattern. It begins with an introduction to the MVC framework and quickly dives into a...

    ASP.NET MVC in Action

    Microsoft's brand new ASP.NET MVC Framework now offers a fully-supported way for developers to implement MVC architectures in ASP.NET applications. ASP.NET MVC in Action is a comprehensive guide to ...

Global site tag (gtag.js) - Google Analytics