图文ASP.Net MVC Razor页面中HtmlHelper帮助程序的写法

2023-10-21 18:33:34 浏览数 (3)

将以下内容复制到cshtml文件中

代码语言:javascript复制
@using Microsoft.AspNetCore.Html
@{
    ViewData["Title"] = "";
}

<p>AntiForgeryToken的使用</p>
@Html.AntiForgeryToken()
<p>ActionLink的使用</p>
<br />
@Html.ActionLink("LinkText", "RazorShow")


@Html.ActionLink("带控制器", "ActionName", "ControllerName")


@Html.ActionLink("带路由信息", "ActionName", new { id = 1, name = 3, age = 4, height = 5 })


<a href="/Html/ActionName/1?name=3&age=4&height=5">带路由信息</a>


@Html.ActionLink("链接", "action", new { id = 1, name = 3, age = 4, height = 5 }, new { @class = "classText", style = "width:200px", tt = "xxx" })


<a class="classText" href="/Home/Index/@DateTime.Now" style="width:200px" tt="xxx">Home</a>
<p>RouteLink的使用</p>


@Html.RouteLink("LinkText", new { action = "ActionName" })

@Html.RouteLink("LinkText", new { action = "ActionName", controller = "ControllerName" })

@Html.RouteLink("LinkText", new { action = "ActionName", id = 1 })



<p>Input 输入框的使用-TextBox</p>
@Html.TextBox("NameId")


@Html.TextBox("NameId", "Value")


@Html.TextBox("NameId", "Value", new { @class = "classText", @style = "width:200px", @tt = "xxx" })

<p>Input 输入框的使用-Hidden</p>
@Html.Hidden("NameId")


@Html.Hidden("NameId", "Value")

<br />
<p>Input 输入框的使用-Password</p>
@Html.Password("NameId")


@Html.Password("NameId", "Value")

<p>Input 输入框的使用-CheckBox</p>
@Html.CheckBox("NameId", true)


@Html.CheckBox("NameId", false)
<p>Input 输入框的使用-RadioButton</p>

@Html.RadioButton("NameId", "Value", true)


@Html.RadioButton("NameId", "Value", false)



<p>Input 输入框的使用-DropDownList,ListBox</p>
@{
    SelectListItem item;
    List<SelectListItem> list = new List<SelectListItem>();
    for (int i = 1; i < 5; i  )
    {
        item = new SelectListItem();
        item.Text = "Text"   i;
        item.Value = "Value"   i;
        item.Selected = (i == 2);
        list.Add(item);
    }
}

@Html.DropDownList("NameId", list)


@Html.ListBox("NameId", list)

<p>表单的Get提交</p>
@using (Html.BeginForm("PostData", "Html", FormMethod.Get))
{
    ;
    @Html.TextBox("UserNameGet")
    ;
    @Html.Password("PasswordGet")
    ;
    <input type="submit" value="SubmitButton" />
}



<p>表单的Post提交</p>
@{
    Html.BeginForm("PostData", "Html", FormMethod.Post);
}

@Html.TextBox("UserNamePost")

@Html.Password("PasswordPost")

<input type="submit" value="SubmitButton" />
@{
    Html.EndForm();
}

生成效果整体图

运行程序,效果图如下

 下面我们对照 html源码,看看生成的html源码。

@Html.AntiForgeryToken

代码语言:javascript复制
@Html.AntiForgeryToken()

生成html源码

代码语言:javascript复制
<input name="__RequestVerificationToken" type="hidden" 
value="CfDJ8BY1L_vJWwdCoEd8zvMkT6Ca2EhsAoVtLk7OoRlpqsHH2ZVCcF2Wl-PaJgsJbV4AgskGDgZ07-kyb1qIT-aW2wkaWPplsUAgdG8URhEskPJIZMDxGAAD5_H-1Iae9eoJ35NSNKMbWQosQM2Jn3idfGM">

@Html.ActionLink

代码语言:javascript复制
@Html.ActionLink("LinkText", "RazorShow")
@Html.ActionLink("带控制器", "ActionName", "ControllerName")
@Html.ActionLink("带路由信息", "ActionName", new { id = 1, name = 3, age = 4, height = 5 })
<a href="/Html/ActionName/1?name=3&age=4&height=5">带路由信息</a>
@Html.ActionLink("链接", "action", new { id = 1, name = 3, age = 4, height = 5 }, 
new { @class = "classText", style = "width:200px", tt = "xxx" })

生成html源码

 @Html.RouteLink

代码语言:javascript复制
@Html.RouteLink("LinkText", new { action = "ActionName" })
@Html.RouteLink("LinkText", new { action = "ActionName", controller = "ControllerName" })
@Html.RouteLink("LinkText", new { action = "ActionName", id = 1 })

生成html源码

 @Html.TextBox

代码语言:javascript复制
@Html.TextBox("NameId")
@Html.TextBox("NameId", "Value")
@Html.TextBox("NameId", "Value", new { @class = "classText", @style = "width:200px", @tt = "xxx" })

 @Html.Hidden

代码语言:javascript复制
@Html.Hidden("NameId")
@Html.Hidden("NameId", "Value")

@Html.Password

代码语言:javascript复制
@Html.Password("NameId")
@Html.Password("NameId", "Value")

@Html.CheckBox

代码语言:javascript复制
@Html.CheckBox("NameId", true)
@Html.CheckBox("NameId", false)

@Html.RadioButton

代码语言:javascript复制
@Html.RadioButton("NameId", "Value", true)
@Html.RadioButton("NameId", "Value", false)

@Html.DropDownList

代码语言:javascript复制
@{
    SelectListItem item;
    List<SelectListItem> list = new List<SelectListItem>();
    for (int i = 1; i < 5; i  )
    {
        item = new SelectListItem();
        item.Text = "Text"   i;
        item.Value = "Value"   i;
        item.Selected = (i == 2);
        list.Add(item);
    }
}
@Html.DropDownList("NameId", list)

 @Html.ListBox

代码语言:javascript复制
@{
    SelectListItem item;
    List<SelectListItem> list = new List<SelectListItem>();
    for (int i = 1; i < 5; i  )
    {
        item = new SelectListItem();
        item.Text = "Text"   i;
        item.Value = "Value"   i;
        item.Selected = (i == 2);
        list.Add(item);
    }
}
@Html.ListBox("NameId", list)

  @Html.BeginForm

代码语言:javascript复制
@using (Html.BeginForm("PostData", "Html", FormMethod.Get))
{
    ;
    @Html.TextBox("UserNameGet")
    ;
    @Html.Password("PasswordGet")
    ;
    <input type="submit" value="SubmitButton" />
}


<p>表单的Post提交</p>
@{
    Html.BeginForm("PostData", "Html", FormMethod.Post);
}

0 人点赞