[代码]Asp.net MVC HtmlHelper类应用实例

baacloud免费翻墙vpn注册使用

尽管MVC framework已经出来几个月了,不过这方面的资料却很少,大部分都是抄来抄去,特别是更细节的对于MVC封装的HtmlHlper类的介绍更是少之又少,有也只是简单的例子,没有涉及到数据库层的实例,实际应用中往往数据都是从数据库中读取出来的,因此,没办法只能啃源码啦,不容易啊,下面是我应用过程中整理的组件绑定数据组件的代码,随着应用会陆续增加。
Html.Select 应用实例:
Conroller

namespace Tang.Controllers
{
public class ChannelController : Controller
{
public void Index()
{
RenderView("Index");
}
public void Manage()
{
RenderView("Index");
}
public void Update()
{
//RenderView();
}
public void New()
{
//读取数据库
SysConst sysConst = SysConst.Instance();
//返回数据集给页面
ViewData["ds"]=sysConst.ChannelSet;
RenderView("Create");
}
}
}

页面:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" AutoEventWireup="true" CodeBehind="Cr&#101;ate.aspx.cs" Inherits="Tang.Views.Channel.Cr&#101;ate" %>
<%@ Import Namespace="Tang.Models.Communion" %>
<%@ Import Namespace="Tang.Controllers" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style type="text/css">
#description {
height: 136px;
width: 494px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<%
string &#91;&#93; states =new string&#91;&#93;{"开放", "关闭","删除"};
//读取数据集,给sel&#101;ct组件赋初值
System.Data.DataSet ds = (System.Data.DataSet)ViewData&#91;"ds"&#93;;
//设置默认选择值
ArrayList sel&#101;ctvalues = new ArrayList();
sel&#101;ctvalues.Add(1);
%>
<form id="Sel&#101;ct" method="post" action="<%=Url.Action(new{}); %>">
<div>
<label>上级频道:</label><%=Html.Sel&#101;ct("parentChannelId", ds, "ChannelName", "Identifier",sel&#101;ctvalues)%>
<label>频道名称:</label><%=Html.TextBox("Channel.ChannelName") %>
<label>频道状态:</label><%=Html.Sel&#101;ct("state", states)%>
<lable>描述:</lable>
<%=Html.TextArea("Channel.Description",null) %>
<div><%=Html.SubmitButton("保存") %></div>
</div>
</form>
</asp:Content>

转载老外的例子:

<%
//Sample Data
string &#91;&#93; songs=new string&#91;&#93;{"Robot Rock (Daft Punk)","Stairway to Heaven (Zeppeling)",
"New Slang (Shins)"};
string &#91;&#93; movies=new string&#91;&#93;{"Tron","Big Trouble In Little China",
"Say Anything"};
string &#91;&#93; zodiac =new string&#91;&#93;{"Aries", "Taurus","Gemini","Cancer","Leo",
"Virgo","Libra","Scorpio","Sag","Capricorn","Aquarius","Haack"};
%>
Sign:
<%=Html.Sel&#101;ct("myZodiac",zodiac) %>
Sign (sel&#101;ct Haacked):
<%=Html.Sel&#101;ct("myZodiac",zodiac,"Haacked") %>
Favorite Movie:
<%=Html.CheckBoxList("favMovie",movies).ToFormattedList("<li>{0}</li>") %>
Favorite Movie, List :
<%=Html.ListBox("favMovie",movies,new string&#91;&#93;{"Say Anything"}) %>
Favorite Movie, List, Long, Mult (sel&#101;ct "Say Anything" and "Tron")i:
<%=Html.ListBox("favMovie",movies,20,true,new string&#91;&#93;{"Say Anything", "Tron"}) %>
Favorite Songs (Sel&#101;ct Shins):
<%=Html.CheckBoxList("favSongs",songs,new string&#91;&#93;{"New Slang (Shins)"})
.ToFormattedList("<li>{0}</li>") %>
Favorite Songs:
<%=Html.CheckBoxList("favSongs", songs).ToFormattedList("<li>{0}</li>")%>
Favorite Songs, Radio:
<%=Html.RadioButtonList("favSongs", songs,"Robot Rock (Daft Punk)")
.ToFormattedList("<li>{0}</li>")%>
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏