下面来实现Customer Controller的Edit方法。在CustomerController.cs中添加如下代码:
public void Edit(string id) { Northwind.Models.NorthwindDataContext dc = new Northwind.Models.NorthwindDataContext(); Customer c = dc.Customers.Single(cus => cus.CustomerID == id);//从数据库中取出参数id所对应的的一个Customer记录 RenderView("Edit", c);//返回Edit View |
相应的在项目中的View/Customer/文件夹下,添加Edit View Edit.aspx:
public partial class Edit : ViewPage<Northwind.Models.Customer> { } |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Edit.aspx.cs" Inherits="Northwind.Views.Customer.Edit" %> <!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></title> </head> <body> <!—下面的 html form 将用户的输入提交到Customer Contoller的Update方法 --> <%using( Html.Form<Northwind.Controllers.CustomerController>(cc=>cc.Update(ViewData.CustomerID))){ %> <div> Customer ID: <%= ViewData.CustomerID %> <br /> Company Nmae: <%= Html.TextBox("Customer.CompanyName", ViewData.CompanyName) %> <br /> Contact Name: <%= Html.TextBox("Customer.ContactName",ViewData.ContactName) %><br /> Contact Title: <%= Html.TextBox("Customer.ContactTitle",ViewData.ContactTitle) %> </div> <%= Html.SubmitButton("Save") %> <%} %> </body> </html> |
代码7中使用了MVC框架中的一个帮助类Html。此类可以生产V