Data Binding in MVC Mode

Source: Internet
Author: User

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 );
}

}
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.