MVC模式下的数据绑定
很怀念Webform下的基于模板的数据绑定。
查了下文档,HtmlHelper并没有相关的直接支持,需要自己在html里迭代生成数据列表。
既然HtmlHelper用的不是很爽,何必舍近求远,要知道ViewPage继承于Page的啊。对于View的局部使用webform的作法,有何不可呢?
[FileLevelControlBuilder(typeof(ViewPageControlBuilder))]
public class ViewPage : Page, IViewDataContainer
{
}
public class ViewPage : Page, IViewDataContainer
{
}
下边是测试代码:
Test1.aspx
<%@ Page Language="C#" Inherits="HelloWorld.Views.Home.Test1" CodeBehind="Test1.aspx.cs"
AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test1</title>
</head>
<body>
<div>
<asp:Repeater runat="server" ID="users">
<ItemTemplate>
id:<%#Eval("Id") %><br />
Name:
<%#Eval("Name") %>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
</div>
</body>
</html>
AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test1</title>
</head>
<body>
<div>
<asp:Repeater runat="server" ID="users">
<ItemTemplate>
id:<%#Eval("Id") %><br />
Name:
<%#Eval("Name") %>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
</div>
</body>
</html>
Test1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HelloWorld.Models;
namespace HelloWorld.Views.Home
{
public partial class Test1 : ViewPage<List<UserModel>>
{
protected void Page_Load(object sender, EventArgs e)
{
users.DataSource = Model;
users.DataBind();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HelloWorld.Models;
namespace HelloWorld.Views.Home
{
public partial class Test1 : ViewPage<List<UserModel>>
{
protected void Page_Load(object sender, EventArgs e)
{
users.DataSource = Model;
users.DataBind();
}
}
}
Test1.aspx.designer.cs
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace HelloWorld.Views.Home {
public partial class Test1 {
/// <summary>
/// users control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Repeater users;
}
}
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace HelloWorld.Views.Home {
public partial class Test1 {
/// <summary>
/// users control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Repeater users;
}
}
HomeController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HelloWorld.Models;
namespace HelloWorld.Controllers
{
public class HomeController : Controller
{
public ActionResult Test1()
{
List<UserModel> users = new List<UserModel>();
users.Add(new UserModel() { Id = "1", Name = "abiao" });
users.Add(new UserModel() { Id = "2", Name = "jim" });
return View(users);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HelloWorld.Models;
namespace HelloWorld.Controllers
{
public class HomeController : Controller
{
public ActionResult Test1()
{
List<UserModel> users = new List<UserModel>();
users.Add(new UserModel() { Id = "1", Name = "abiao" });
users.Add(new UserModel() { Id = "2", Name = "jim" });
return View(users);
}
}
}