星空网 > 软件开发 > ASP.net

第19章 辅助器方法

* 辅助器方法可以对代码块和标记进行打包,以便在整个MVC框架应用程序中重用。

一、自定义辅助器方法

  1.内联辅助器方法

    使用@helper标签,具有名称、参数,无返回值,结果直接放到客户端的响应之中。在运行时评估类型(自动转换)

@helper ListArrayItems(string[] items){  <ul>  @foreach (string str in items)  {    <li>@str</li>  }  </ul>}

<div>  水果在这:@ListArrayItems(ViewBag.Fruits)</div>

  2.外部辅助器方法(扩展方法:对HtmlHelper对象进行扩展,HTML辅助器方法结果是一个MvcHtmlString对象)

 public static MvcHtmlString ListArrayItems(this HtmlHelper html , string[] lists)    {       TagBuilder tag= new TagBuilder("ul");      foreach (string str in lists)      {        TagBuilder itemTag = new TagBuilder("li");        itemTag.SetInnerText(str);        tag.InnerHtml += itemTag.ToString();      }      return new MvcHtmlString(tag.ToString());    }

@using HelperMethods.Infrastructure
.......
<div>  城市在这:@Html.ListArrayItems((string[])ViewBag.Cities)  </div>

 

HtmlHelper类定义的有用的属性

属性描述
RouteCollection返回应用程序定义的路由集合
ViewBag返回视图包数据,从动作方法传递给调用辅助器方法的视图
ViewContext返回ViewContext对象,对请求的细节以及请求的处理方式进行访问

ViewContext类定义的有用属性

属性描述
Controller返回处理当前请求的控制器
HttpContext返回描述当前请求的HttpContext对象
IsChildAction当是子动作渲染的视图时为true
RouteData返回请求路由数据
View返回已调用辅助器方法的IView实现的实例

TagBuilder(创建HTML)类的一些成员

成员描述
InnerHtml将元素内容设置成HTML字符串的一个属性,赋给这个属性的值将不进行编码,可以将它嵌入HTML元素
SetInnerText(string)设置HTML元素的文本内容。string参数将被编码,以安全显示
AddCssClass(string)对HTML元素添加一个CSS的class
MergeAttribute(string,string,bool)对HTML元素添加一个标签属性。(参数是标签属性名称,值,是否替换已存在的同名标签属性)

     命名空间的引入可以放在/Views/web.config中。

    辅助器方法:为了减少视图中的重复量,只用于最简单的内容。

    分部视图:用于更复杂的标记和内容

    子动作:需要实现对模型数据的操作时使用。(如果辅助器含有大量的C#语句C#语句多于HTML元素时建议使用子动作)   

    对辅助器方法中的(危险)数据进行编码:(Mvc框架会认为辅助器方法生成的内容是安全的) 

      //有选择地对辅助器方法中的数据值进行编码       string encodedMsg = html.Encode(msg);      string result = string.Format("这里是信息内容:<p>{0}</p>", encodedMsg);      return new MvcHtmlString(result);

二、内建的Form辅助器方法

  1.创建表单元素

BeginForm辅助器方法的重载

重载描述
BeginForm()创建一个表单,回递给源动作方法(引发渲染该表单的动作依法)
BeginForm(action,controller)创建一个表单,回递给以字符串形式指定的动作方法和控制器
BeginForm(action,controller,method)创建一个表单,回递给以字符串形式指定的动作方法和控制器,并指定form元素中method标签属性的值
BeginForm(action,controller,method,attributes)创建一个表单,回递给以字符串形式指定的动作方法和控制器,并指定form元素中method标签属性的值,指定form元素的标签属性(对象的属性)
BeginForm(action,controller,routeValues,method,attributes)创建一个表单,回递给以字符串形式指定的动作方法和控制器,并指定form元素中method标签属性的值,指定form元素的标签属性(对象的属性),为路由配置中的路由片段变量指定一个值(对象的属性对应路由变量)
@using (Html.BeginForm(  "CreatePerson", "Home", //动作方法名称,控制器名  new { id = "MyIdValue" },//路由配置中id片段变量的值  FormMethod.Post,//method标签属性的值  new { @class="personClass",data_formType="person"}//form元素其他标签属性  )){  ............  <input type="submit" value="提交"/>  }
// <form action="/Home/CreatePerson/MyIdValue" html-attribute-value">personClass" data-formType="person" method="post">

          data_formType="person" 会被自动的映射成  data-formType="person"

   指定表单使用路由:

@using (Html.BeginRouteForm(  "Default",//路由名称  new { id = "MyIdValue" },//路由配置中id片段变量的值  FormMethod.Post,//method标签属性的值  new { @class="personClass",data_formType="person"}//form元素其他标签属性 )){

  2.Input辅助器

基本Input HTML辅助器(强类型结果未输出,相似)

HTML元素示例输出

Checkbox

(复选框,检查框)

@Html.CheckBox("myCheckbox", false)
@Html.CheckBorFor(x=>x.myCheckbox)

<input id="myCheckbox" name="myCheckbox" type="checkbox" value="true" />

<input name="myCheckbox" type="hidden" value="false" />

Hidden field

(隐藏字段)

@Html.Hidden("myHidden", "val")
@Html.HiddenFor(x=>x.myCheckbox)
<input id="myHidden" name="myHidden" type="hidden" value="val" />

Radio button

(单选按钮)

@Html.RadioButton("myRadioButton", "val", true)
@Html.RadioButtonFor(x=>x.myRadioButton, "val")
<input checked="checked" id="myRadioButton" name="myRadioButton" type="radio" value="val" />

Password

(密码框)

@Html.Password("myPassword", "val")
@Html.PasswordFor(x=>x.myPassword)
<input id="myPassword" name="myPassword" type="password" value="val" />

Text area

(文本域)

@Html.TextArea("myTextArea", "val", 5, 20, null)
@Html.TextAreaFor(x=>x.myTextArea, 5,20,new{})
<textarea cols="20" id="myTextArea" name="myTextArea" rows="5">

Text box

(文本框)

@Html.TextBox("myTextBox", "val")
@Html.TextBoxFor(x=>x.myTextBox)
<input id="myTextBox" name="myTextBox" type="text" value="val" />
Select元素

Drop-down list

(下拉列表)

 
 @Html.DropDownList("myList",new SelectList(new[]{"A","B"}),"请选择")

<select id="myList" name="myList">

<option value="">请选择</option>
<option>A</option>
<option>B</option>
</select>

Drop-down list

(强类型下拉列表)

 @Html.DropDownListFor(m=>m.Gender,  new SelectList(new[]{"M","F"}))        

<select id="Gender" name="Gender">
<option>M</option>
<option>F</option>
</select>

Multiple-select

(多项选择)

@Html.ListBox("myList",new MultiSelectList(new []{"A","B"}))

<select id="myList" multiple="multiple" name="myList">

<option>A</option>

<option>B</option>

</select>

Multiple-select

(强类型多项选择)

@Html.ListBoxFor(x=>x.Vals,new MultiSelectList(new []{"A","B"}))

<select id="Vals" multiple="multiple" name="Vals">

<option>A</option>

<option>B</option>

</select>

   注:Checkbox 渲染了两个input元素(检查框和同名的隐藏input元素),因为浏览器在检查框未作出选择时,不会递交检查框的值,通过隐藏控件确保MVC框架在作出选择后

从隐藏字段获得一个值。

     selec t辅助器以(基于IEnumerable对象序列)SelectList或MultiSelectList为参数(构造器选项可以在初始化时指定多个选择初值)。这两个对象能够为列表项提取对象(包括模型对象)的值。属性值和select元素的源List中的值是同一类型(来自于同一个类)。




原标题:第19章 辅助器方法

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流