Mvc html helper

Source: Internet
Author: User
Tags actionlink

First, ASP. net mvc can be usedHtml HelperHowever, using HTML helper can save a lot of time for O (hour _ hour) O ~

I. Standard HTML helper

.Actionlink

Create a link, but you cannot create a link with an image

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %><asp:ContentID="indexContent"ContentPlaceHolderID="MainContent"runat="server"><p>To learn more about this website, click the following link:<%= Html.ActionLink("About this Website","About")%></p></asp:Content>
The content displayed in "Aboud this Website" and the name of "about" Action
The generated HTML is as follows:
<ahref="/Home/About">About this Website</a>
Actionlink can be added to accept many parameters

· Linktext-text on the Link

· Actionname-action name of The Link Target

· Routevalues-route value leading to action

· Controllername-controller name

· Htmlattributes-link HTML attributes

· Protocol-connection protocol (for example, https)

· Hostname-Host Name of The Link (for example, www.mywebsite.com)

· Fragment-this is not too explicit (too explicit) yet

To add an image link, use URL. Action:

<ahref="<%= Url.Action("Delete") %>"></a>
 
HTML helper can also generate many HTML controls:

· Beginform ()

· Checkbox ()

· Dropdownlist ()

· Endform ()

· Hidden ()

· ListBox ()

· Password ()

· Radiobutton ()

· Textarea ()

· Textbox ()

 

You can see the name. For example:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Customer>" %><asp:ContentID="Content2"ContentPlaceHolderID="MainContent"runat="server"><%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.")%><%using(Html.BeginForm()) {%><fieldset><legend>Register</legend><p><labelfor="FirstName">First Name:</label><%= Html.TextBox("FirstName")%><%= Html.ValidationMessage("FirstName","*")%></p><p><labelfor="LastName">Last Name:</label><%= Html.TextBox("LastName")%><%= Html.ValidationMessage("LastName","*")%></p><p><labelfor="Password">Password:</label><%= Html.Password("Password")%><%= Html.ValidationMessage("Password","*")%></p><p><labelfor="Password">Confirm Password:</label><%= Html.Password("ConfirmPassword")%><%= Html.ValidationMessage("ConfirmPassword","*")%></p><p><labelfor="Profile">Profile:</label><%= Html.TextArea("Profile",new{cols=60, rows=10})%></p><p><%= Html.CheckBox("ReceiveNewsletter")%><labelfor="ReceiveNewsletter"style="display:inline">Receive Newsletter?</label></p><p><inputtype="submit"value="Register"/></p></fieldset><%}%></asp:Content>

Here, HTML. beginform () and endform () should be said separately: by default, it will point to the same action as itself, but it will also accept the action to which different parameters change:

· Routevalues -- As shown above

· Actionname-as shown above

· Controllername-as shown above

· Method-only post and get can be used, and JavaScript must be used

· Htmlattributes-above

 

. Encode (),Replace <with & lt;> with & gt; and so on.

.AntiforgerytokenThis is to defend against cross-origin attacks.

<%= Html.AntiForgeryToken()%>

<inputname="__RequestVerificationToken"type="hidden"value="6tbg3PWU9oAD3bhw6jZwxrYRyWPhKede87K/PFgaw     6MI3huvHgpjlCcPzDzrTkn8"/>
Helper will create a cookie and compare it with the value of this hidden domain
Write the following in controller:CodeYou can:
usingSystem.Web.Mvc;namespaceMvcApplication1.Controllers  {publicclassBankController : Controller      {//// GET: /Bank/WithdrawpublicActionResult Withdraw()          {returnView();          }//// POST: /Bank/Withdraw[AcceptVerbs(HttpVerbs.Post)]          [ValidateAntiForgeryToken]publicActionResult Withdraw(decimalamount)          {// Perform. withdrawalreturnView();          }        }  }

Create your own HTML helpers

usingSystem;usingSystem.Web.Mvc;namespaceHelpers  {publicstaticclassSubmitButtonHelper      {/// <summary>/// Renders an HTML form. submit button/// </summary>publicstaticstringSubmitButton(thisHtmlHelper helper,stringbuttonText)          {returnString.Format("<input type=/"submit/" value=/"{0}/" />", buttonText);          }        }  }

Create a submit. (* ^__ ^ *)

This allows you to create complex HTML formats. Give full play to your imagination.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.