2、HtmlHelper类

[b]HtmlHelper类[/b]

[b]<%Html.[/b]

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

[b]Html.Encode[/b]
等价于:
<%:%>

[b]Html.TextBox[/b]
<%: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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值