The following sample code is not functional, it is just for illustration. The documentation and a working sample project is on the way. Please be patient.

All it takes to use the generic MvcControl (within a view) is code like this:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<System.Collections.Generic.IEnumerable<CustomerModel>>" %>
<%@ Import Namespace="MyCompany.Web.Mvc" %>

<!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>Sample use case of the generic MvcControl</title>
<script type="text/javascript" language="javascript">
function ShowCustomersForCompany(companyID, targetGridID)
{
// do something here to show the customers for the specified company
}
</script>
</head>
<body>

<h1>My Customers</h1>

<%: MvcControl<DropDownList>.Render(c=>
{
c.ID = "CompanyList";
c.DataTextField = "CompanyName";
c.DataValueField = "CompanyID";
c.Attributes["onchange"] = "javascript: ShowCustomersForCompany(this.value, 'CustomersGridView');";

c.DataSource = ViewData["CompanyList"];
c.DataBind();
})%>

<br />

<%: MvcControl<GridView>.Render(c=>
{
c.ID = "CustomersGridView";
c.AutoGenerateColumns = false;

c.Columns.Add(new CommandField() { ShowSelectButton = true });
c.Columns.Add(new BoundField() { DataField = "CustomerName", HeaderText = "Customer name", ReadOnly = true });
c.Columns.Add(new BoundField() { DataField = "CompanyName", HeaderText = "Company", ReadOnly = true });
c.Columns.Add(new BoundField() { DataField = "LastUpdated", HeaderText = "Last activity", ReadOnly = true });

c.SelectedRowStyle.BackColor = System.Drawing.Color.Navy;
c.SelectedRowStyle.ForeColor = System.Drawing.Color.White;

c.RowDataBound += (sender, e) =>
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CustomerModel cust = (CustomerModel)e.Row.DataItem;
// the first cell of the row is a command field
e.Row.Cells[0].Text = string.Format("<a href=\"/Customers/Edit/{0}\">Edit</a>", cust.ID);
}
};

c.DataSource = this.Model; // Note that the view has a model of type System.Collections.Generic.IEnumerable<CustomerModel>
c.DataBind();
})%>

</body>
</html>

Last edited Jul 20, 2010 at 1:20 AM by Bigabdoul, version 2

Comments

No comments yet.