尽管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="Create.aspx.cs" Inherits="Tang.Views.Channel.Create" %>
<%@ 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 [] states =new string[]{"开放", "关闭","删除"};
//读取数据集,给select组件赋初值
System.Data.DataSet ds = (System.Data.DataSet)ViewData["ds"];
//设置默认选择值
ArrayList selectvalues = new ArrayList();
selectvalues.Add(1);
%>
<form id="Select" method="post" action="<%=Url.Action(new{}); %>">
<div>
<label>上级频道:</label><%=Html.Select("parentChannelId", ds, "ChannelName", "Identifier",selectvalues)%>
<label>频道名称:</label><%=Html.TextBox("Channel.ChannelName") %>
<label>频道状态:</label><%=Html.Select("state", states)%>
<lable>描述:</lable>
<%=Html.TextArea("Channel.Description",null) %>
<div><%=Html.SubmitButton("保存") %></div>
</div>
</form>
</asp:Content>
转载老外的例子:
<%
//Sample Data
string [] songs=new string[]{"Robot Rock (Daft Punk)","Stairway to Heaven (Zeppeling)",
"New Slang (Shins)"};
string [] movies=new string[]{"Tron","Big Trouble In Little China",
"Say Anything"};
string [] zodiac =new string[]{"Aries", "Taurus","Gemini","Cancer","Leo",
"Virgo","Libra","Scorpio","Sag","Capricorn","Aquarius","Haack"};
%>
Sign:
<%=Html.Select("myZodiac",zodiac) %>
Sign (select Haacked):
<%=Html.Select("myZodiac",zodiac,"Haacked") %>
Favorite Movie:
<%=Html.CheckBoxList("favMovie",movies).ToFormattedList("<li>{0}</li>") %>
Favorite Movie, List :
<%=Html.ListBox("favMovie",movies,new string[]{"Say Anything"}) %>
Favorite Movie, List, Long, Mult (select "Say Anything" and "Tron")i:
<%=Html.ListBox("favMovie",movies,20,true,new string[]{"Say Anything", "Tron"}) %>
Favorite Songs (Select Shins):
<%=Html.CheckBoxList("favSongs",songs,new string[]{"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>")%>
Mikel