Asp.Net MVC learning experience of Html Helper

The first to use Asp.Net MVC can not use the Html Helper, but use the Html Helper can save a lot of time O (∩_∩) O ~

First, the standard Html Helper

.ActionLink

Create a link, but now can not create a link with pictures

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>  
<asp:Content ID="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>  
"Aboud this Website”显示的内容,"About” Action的名字
生成的Html如下:
<a href="/Home/About">About this Website</a>
ActionLink可以添加接受很多参数

· LinkText - text link

· ActionName - action name link target

· RouteValues ​​- the route leading to the action value

· controllerName – controller名字

· HtmlAttributes - linked html attributes

· Protocol - link protocol (for example: https)

· Hostname - Host name of the link ( for example: www.mywebsite.com )

· Fragment - this did not get quite understand ╮ (╯ ▽ ╰) ╭

If you want to add a picture link, use Url.Action:

<a href="<%= Url.Action("Delete") %>"><img src="http://www.cnblogs.com/Content/Delete.png" alt="Delete" style="border:0px" /></a>
Html Helper还可以生成很多Html控件:

· BeginForm()

· CheckBox()

· DropDownList()

· EndForm()

· Hidden()

· ListBox()

· Password()

· RadioButton()

· TextArea()

· TextBox()

 

Basically see the name to know, look at an example:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Customer>" %>  
<asp:Content ID="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>  
                <label for="FirstName">First Name:</label>  
                <%= Html.TextBox("FirstName") %>  
                <%= Html.ValidationMessage("FirstName", "*") %>  
            </p>  
            <p>  
                <label for="LastName">Last Name:</label>  
                <%= Html.TextBox("LastName") %>  
                <%= Html.ValidationMessage("LastName", "*") %>  
            </p>  
            <p>  
                <label for="Password">Password:</label>  
                <%= Html.Password("Password") %>  
                <%= Html.ValidationMessage("Password", "*") %>  
            </p>  
            <p>  
                <label for="Password">Confirm Password:</label>  
                <%= Html.Password("ConfirmPassword") %>  
                <%= Html.ValidationMessage("ConfirmPassword", "*") %>  
            </p>  
            <p>  
                <label for="Profile">Profile:</label>  
                <%= Html.TextArea("Profile", new {cols=60, rows=10})%>  
            </p>  
            <p>  
                <%= Html.CheckBox("ReceiveNewsletter") %>  
                <label for="ReceiveNewsletter" style="display:inline">Receive Newsletter?</label>  
            </p>  
            <p>  
                <input type="submit" value="Register" />  
            </p>  
        </fieldset>  
  
    <% } %>  
  
</asp:Content>  

Which Html.BeginForm () and EndForm () alone will say this: By default, it will point to their own and the same action, but will accept different parameters to change the point of action:

· RouteValues ​​- as above

· ActionName - as above

· ControllerName - as above

· Method - Use only POST and GET, you must use javascript

· HtmlAttributes - as above

 

.Encode (), this is replaced <is lt &;> is a & gt; and the like

. AntiForgeryToken This is to resist cross-domain attacks of.

<%= Html.AntiForgeryToken() %>

Generates a hidden field, value is different each time the random string, such as:

<input name="__RequestVerificationToken"  type="hidden"value="6tbg3PWU9oAD3bhw6jZwxrYRyWPhKede87K/PFgaw
     6MI3huvHgpjlCcPzDzrTkn8" />
helper会创建一个cookie和这个隐藏域的值相比较
在Controller中如下写代码就可以了:
using System.Web.Mvc;  
  
namespace MvcApplication1.Controllers  
{  
    public class BankController : Controller  
    {  
        //  
        // GET: /Bank/Withdraw  
  
        public ActionResult Withdraw()  
        {  
            return View();  
        }  
  
        //  
        // POST: /Bank/Withdraw  
        [AcceptVerbs(HttpVerbs.Post)]  
        [ValidateAntiForgeryToken]  
        public ActionResult Withdraw(decimal amount)  
        {  
            // Perform withdrawal  
            return View();  
        }  
  
    }  
} 

Create your own HTML Helpers

using System;  
using System.Web.Mvc;  
  
namespace Helpers  
{  
    public static class SubmitButtonHelper  
    {  
        /// <summary>  
        /// Renders an HTML form submit button  
        /// </summary>  
        public static string SubmitButton(this HtmlHelper helper, string buttonText)  
        {  
            return String.Format("<input type=\"submit\" value=\"{0}\" />", buttonText);  
        }  
  
    }  
}  

This name it, create a submit. (* ^ __ ^ *)

This can create very complex Html formats. Use your imagination

ps: someone else's code or

Reproduced in: https: //www.cnblogs.com/dotLive/archive/2009/03/09/1407188.html

Guess you like

Origin blog.csdn.net/weixin_34342992/article/details/93765162