Asp.Net MVCは多くの時間を節約することができますO(∩_∩)O〜のHTMLヘルパーを使用しますが、Htmlのヘルパーを使用することはできません使用する最初の
まず、標準的なHTMLヘルパー
。ActionLinkの
リンクを作成しますが、今の画像とのリンクを作成することはできません。
<%@ 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 - テキストリンク
・ACTIONNAME - アクション名のリンク先
・RouteValues - 行動価値につながるルート
・コントローラ名 - コントローラ名字
・HtmlAttributes - リンクHTML属性
・プロトコル - リンクプロトコル(例:https)を
・ホスト名-リンクのホスト名(例: www.mywebsite.com)
・フラグメント - これは╮かなり理解し得なかった(╯▽╰)╭
あなたは絵のリンクを追加したい場合は、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()
・チェックボックス()
・ ドロップダウンリスト()
・エンドフォーム()
・隠し()
・ リストボックス()
・パスワード()
・ ラジオボタン()
・のTextArea()
・ テキストボックス()
基本的には例を見て、知っている名前を参照してください。
<%@ 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>
どのHtml.BeginForm()とENDFORM()だけではこれを言うだろう:デフォルトでは、それは自分自身と同じアクションを指しますが、作用点を変更するために異なるパラメータを受け入れます。
・RouteValues - 上記のように
・ACTIONNAME - 上記のように
・コントローラ名 - 上記のように
・メソッド - のみを使用してPOSTとGET、あなたはJavaScriptを使用しなければなりません
・HtmlAttributes - 上記のように
.Encode()、これは置換されている<LTであると、>は&gt;タグと等
。AntiForgeryTokenこれは抵抗することで、クロスドメイン攻撃のを。
<%= Html.AntiForgeryToken() %>
値は以下のようなランダムな文字列、毎回異なり、隠しフィールドを生成します。
<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();
}
}
}
独自のHTMLヘルパーを作成します。
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);
}
}
}
この名前には、提出を作成します。(* ^ __ ^ *)
これは非常に複雑なHTML形式を作成することができます。あなたの想像を使用してください
PS:誰か他の人のコードや
ます。https://www.cnblogs.com/dotLive/archive/2009/03/09/1407188.htmlで再現