2、HtmlHelper类

HtmlHelper类

<%Html.

MVC1
<%:Html.TextBoxFor("PropertyName")%>
MVC2
<%:Html.TextBoxFor(m=>m.PropertyName)%>

Html.Encode
等价于:
<%:%>

Html.TextBox
<%:Html.TextBox("name")%>
等价于
<input id="name" name="name" type="text" value=""/>

强类型
public ActionResult Edit(int id)
{
Var product =new Product{Name="danny"}
ViewData["Product"]=product;
return View();
}

View层
<%:Html.TextBox("Product.Name")%>

Html显示
<input id="Product_name" name="Product.Name" type="text" value="danny"/>
----------------------------------------
<%:Html.TextBox("Name","danny")%>

<%:Html.TextBoxFor(m=>m.Name,null,new{@class="cl1"})%>
<input class="cl1" id="Name" name="Name" type="text" value="danny"/>

Html.ActionLink和Html.RouteLink
这两控件都根据用户输入生成导航信息
ActionLink根据生成调用另外Controller的链接

调用同一个controller的另一个action
<%:Html.ActionLink("链接文字","anotherAction")%>
运行结果
<a href="/Home/anotherAction">链接文字</a>


另一个controller的另一个action
<%:Html.ActionLink("链接文字","Withdraw","Account")%>

<%:Html.ActionLink("链接文字","Withdraw","Account",new{id=34231},null)%>
<%:Html.ActionLink("链接文字","About","Account",null,new{title="about"})%>

RouteLink
<%:Html.RouteLink("链接文字",new{action="anotherAction"})%>


Html.BeginForm
完全模拟<form>标记
1、
<%using(Html.BeginForm(){%>
一些Html元素,比如textbox
<%}%>
2、
<%Html.BeginForm();%>
一些Html元素,比如textbox
<%Html.EndForm();%>

Html.Hidden
用于生成隐藏文本
<%:Html.Hidden("guidno","xx-xx-xx")%>
输出结果
<input id="guidno" name="guidno" type="hidden" value="xx-xx-xx"/>
强类型支持
<%:Html.HiddenFor(m=>m.Guid)%>

Html.DropDownList和Html.ListBox
用于显示列表或选择值
用于限制用户的输入

public ActionResult Edit(int id){
  Nodb context=new Nodb();
  var product=context.ProductS.Single(p=>p.ProductID==id);
ViewData["CategoryId"]=from c in p.Categrories
                               select new SelectListItem{
                                  Text =c.CategoryName,
                                   value=c.CategroyID,
                                   selected=(c.CategoryID==p.CategoryID)
    };
return View(Product);
}

<%:Html.DropDownList("CategoryId")%>
强数据类型
<%:Html.DropDownListFor(m=>m.categoryid)%>

Html.Password
Password的工作方式和textbox一样,只是提交以后数据会自动清空
<%:Html.Password("upwd")%>
返回
<input id="my password" name="upwd" type="password" value="*"/>
强类型支持
<%:Html.Password(m=>m.upwd)%>

Html.RadioButton
用户选择其一
<%:Html.RadioButton("color","red")%>
<%:Html.RadioButton("color","yellow","true")%>
结果
<input id="color" name="color" type="radio" value="red">
<input checked="checked" id="color" name="color" type="radio" value="yellow">

Html.Partial和Html.RenderPartial
用于输出不完整的Html片段
aspx页面可以

public void Partial(String partialViewName);
public void Partial(String partialViewName,object model);
public void Partial(String partialViewName,ViewDataDictionary viewData);
public void Partial(String partialViewName,object model,ViewDataDictionary viewData);


<%:Html.Partial("MyUserControl");%>
<%Html.RenderPartial("MyUserControl");%> //html response.write

Html.Action和Html.RenderAction
用于实现指定controller的指定的Action调用
public class MyController{
  public ActionResult Index(){
  return View();
  }
[ChildActionOnly]
public ActionResult Menu(){
  var menu=GetMenu();
return PartialView(menu);
}
}

<%@Control Inherits="System.Web.Mvc.ViewUserControl<Menu>"%>

<ul>
  <%foreach(var item in Model.MenuItem){%>
     <li><%=item%></li>
<%}%>
</ul>


<%=Html.Action("Menu")%>
不能用URL直接调用。

RenderAction
传递参数
public class MenuOptions{
  public int Width{get;set;}
  public int Height{get;set;}
}

[ChildActionOnly]
[ActionName="coolMenu"]
public ActionResult Menu(MenuOptions options){
  return PartialView(options);
}

<%=Html.Action("Menu",new{options=new MenuOptions{width=400,Height=500}})%>
2011-4-14 22:52 danny

你可能感兴趣的:(html,C++,c,mvc,C#)