We miss template-based Data Binding in webform.
After checking the document, htmlhelper does not have relevant direct support. You need to iteratively generate a data list in HTML.
Since htmlhelper is not very good, why should we leave it close to the distance? We need to know That viewpage inherits from page. How can we avoid using webform for partial view?
[Filelevelcontrolbuilder (typeof (viewpagecontrolbuilder)]
Public class viewpage: Page, iviewdatacontainer
{
}
Below is the test code:
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>
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 ();
}
}
}
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;
}
}
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 );
}
}
}