ASP.NET - Razor HTML Helpers

ASP.NET - Razor HTML Helpers 首页 / ASP.Net MVC入门教程 / ASP.NET - Razor HTML Helpers

HtmlHelper是MVC 2中引入的一个类,用于以编程方式创建HTML控件。它提供了在视图页上生成控件的内置方法。在本主题中,无涯教程列出了该类的构造函数、属性和方法。

Note: HTMLHelper类旨在生成UI。它不应用于控制器或模型类。

构造函数

以下是HtmlHelper类的构造函数。

Constructor NameDescription
HtmlHelper(ViewContext, IViewDataContainer)它使用指定的视图上下文和查看数据容器初始化HTMLHelper类的新实例。
HtmlHelper(ViewContext, IViewDataContainer, RouteCollection)它使用指定的视图上下文,查看数据容器和路由集合初始化HTMLHelper类的新实例。

属性

NameDescription
RouteCollection它用于获取或设置应用程序的路由集合。
ViewBag它用于获得视图包。
ViewContext它用于获取或设置视图的上下文信息。
ViewData它用于获取当前视图数据字典。
ViewDataContainer它用于获取或设置视图数据容器。

HtmlHelper扩展方法

NameDescriptionOverloaded Methods
Action(String)它用于调用指定的子操作方法,并将结果返回为HTML字符串。Action(String, Object) Action(String, RouteValueDictionary) Action(String, String) Action(String, String, Object) Action(String, String, RouteValueDictionary)
BeginForm()它用于生成一个开口<form>标记。该表单使用POST方法。BeginForm(Object) BeginForm(RouteValueDictionary) BeginForm(String, String) BeginForm(String, String, FormMethod) BeginForm(String, String, FormMethod, IDictionary<String, Object>) BeginForm(String, String, FormMethod, Object) BeginForm(String, String, Object) BeginForm(String, String, Object, FormMethod) BeginForm(String, String, Object, FormMethod, Object) BeginForm(String, String, RouteValueDictionary) BeginForm(String, String, RouteValueDictionary, FormMethod) BeginForm(String, String, RouteValueDictionary, FormMethod, IDictionary<String, Object>)
CheckBox(String)它用于通过使用指定的HTML Helper和表单字段的名称来生成复选框输入元素。CheckBox(String, Boolean) CheckBox(String, Boolean, IDictionary<String, Object>) CheckBox(String, Boolean, Object) CheckBox(String, IDictionary<String, Object>) CheckBox(String, Object)
DropDownList(String)它使用指定的HTML Helper和表单字段的名称生成单个选择元素。DropDownList(String, IEnumerable<SelectListItem>) DropDownList(String, IEnumerable<SelectListItem>, IDictionary<String, Object>) DropDownList(String, IEnumerable<SelectListItem>, Object) DropDownList(String, IEnumerable<SelectListItem>, String) DropDownList(String, IEnumerable<SelectListItem>, String, IDictionary<String, Object>) DropDownList(String, IEnumerable<SelectListItem>, String, Object) DropDownList(String, String)
Editor(String)它为由表达式表示的对象中的每个属性生成HTML输入元素。Editor(String, Object) Editor(String, String) Editor(String, String, Object) Editor(String, String, String) Editor(String, String, String, Object)
EndForm()它呈现关闭</ form>标记。
Label(String)它生成HTML标签元素。Label(String, IDictionary<String, Object>) Label(String, Object) Label(String, String) Label(String, String, IDictionary<String, Object>) Label(String, String, Object)
ListBox(String)它使用指定的HTML Helper返回多选选择元素。ListBox(String, IEnumerable<SelectListItem>) ListBox(String, IEnumerable<SelectListItem>, IDictionary<String, Object>) ListBox(String, IEnumerable<SelectListItem>, Object)
Password(String)它用于通过使用指定的HTML帮助程序生成密码输入元素。Password(String, Object) Password(String, Object, IDictionary<String, Object>) Password(String, Object, Object)
RadioButton(String, Object)它返回一个单选按钮输入元素。RadioButton(String, Object, Boolean) RadioButton(String, Object, Boolean, IDictionary<String, Object>) RadioButton(String, Object, Boolean, Object) RadioButton(String, Object, IDictionary<String, Object>) RadioButton(String, Object, Object)
TextArea(String)它用于将文本区域创建到网页。TextArea(String, IDictionary<String, Object>) TextArea(String, Object) TextArea(String, String) TextArea(String, String, IDictionary<String, Object>) TextArea(String, String, Int32, Int32, IDictionary<String, Object>) TextArea(String, String, Int32, Int32, Object) TextArea(String, String, Object)
TextBox(String)它用于通过使用指定的HTML帮助程序返回文本输入元素。TextBox(String, Object) TextBox(String, Object, IDictionary<String, Object>) TextBox(String, Object, Object) TextBox(String, Object, String) TextBox(String, Object, String, IDictionary<String, Object>) TextBox(String, Object, String, Object)

示例

因为这是一个MVC应用程序,所以,它包括模型,视图和仪器。此示例包括以下文件。

//HtmlHelperDemo.cshtml

@{  
    ViewBag.Title = "HtmlHelperDemo";  
}  
<hr />  
<h3>User Registration Form</h3>  
<hr />  
<div class="form-horizontal">  
    @using (Html.BeginForm("UserRegistration", "Students"))  
    {  
        <div class="form-group">  
            @Html.Label("User Name", new { @class = "control-label col-sm-2" })  
            <div class="col-sm-10">  
                @Html.TextBox("username", null, new { @class = "form-control" })  
            </div>  
        </div>  
        <div class="form-group">  
            @Html.Label("Email ID", new { @class = "control-label col-sm-2" })  
            <div class="col-sm-10">  
                @Html.TextBox("email", null, new { @class = "form-control" })  
            </div>  
        </div>  
        <div class="form-group">  
            @Html.Label("Gender", new { @class = "control-label col-sm-2" })  
            <div>  
                <div class="col-sm-10">  
                    Male @Html.RadioButton("Gender", "male")  
                    Female @Html.RadioButton("Gender", "female")  
                </div>  
            </div>  
        </div>  
                <div class="form-group">  
                    @Html.Label("Courses", new { @class = "control-label col-sm-2" })  
                    <div class="col-sm-10">  
                        C# @Html.CheckBox("C#", new { value = "C#" })  
                        ASP.NET @Html.CheckBox("ASP.NET", new { value = "ASP.NET" })  
                        ADO.NET @Html.CheckBox("ADO.NET", new { value = "ADO.NET" })  
                    </div>  
                </div>  
                <div class="form-group">  
                    @Html.Label("Contact", new { @class = "control-label col-sm-2" })  
                    <div class="col-sm-10">  
                        @Html.TextBox("contact", null, new { @class = "form-control" })  
                    </div>  
                </div>  
                <div class="form-group">  
                    <div class="col-sm-2"></div>  
                    <div class="col-sm-10">  
                        <input type="submit" value="submit" class="btn btn-primary" />  
                    </div>  
                </div>  
    }  
</div>  

控制器

// StudentsController.cs

using System.Web.Mvc;
namespace MvcApplicationDemo.Controllers
{
    public class StudentsController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult HtmlHelperDemo()
        {
            return View();
        }
[HttpPost]
        public ContentResult UserRegistration()
        {
            return Content(
                "User Name = " + Request.Form["username"] + "" +
                "Email ID  = " + Request.Form["email"] + "" +
                "Gender    = " + Request.Form["gender"] + "" +
                "Courses   = " + Request.Form.GetValues("C#")[0] + " " + Request.Form.GetValues("ASP.NET")[0] + " " + Request.Form.GetValues("ADO.NET")[0] + "" +
                "Contact   = " + Request.Form["contact"] + ""
                );
        }
    }
}

输出:

ASP Net razor html helpers 1

无涯教程正在提交此表格,其中包含以下详细信息。

链接:https://www.learnfk.comhttps://www.learnfk.com/asp.net_mvc/asp-net-razor-html-helpers.html

来源:LearnFk无涯教程网

ASP Net razor html helpers 2

提交后,将收到行动方法中的所有值。

ASP Net razor html helpers 3

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

数据结构与算法之美 -〔王争〕

Go语言从入门到实战 -〔蔡超〕

OpenResty从入门到实战 -〔温铭〕

JavaScript核心原理解析 -〔周爱民〕

物联网开发实战 -〔郭朝斌〕

A/B测试从0到1 -〔张博伟〕

React Hooks 核心原理与实战 -〔王沛〕

eBPF核心技术与实战 -〔倪朋飞〕

AI大模型企业应用实战 -〔蔡超〕

好记忆不如烂笔头。留下您的足迹吧 :)