用MVC模式重写时遇到的问题
数据库为Northwind,要求在显示customer的gridview上点击显示该顾客有关的order信息protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int index = gvrow.RowIndex;
string oid = GridView1.Rows[index].Cells[1].Text.Trim();
NorthwindDataContext db = new NorthwindDataContext();
var orders = from s in db.Order
from p in db.Order_Detail
where s.CustomerID == oid
&& s.OrderID == p.OrderID
select new
{
CustomerID = s.CustomerID,
OrderID = s.OrderID,
OrderDate = s.OrderDate,
RequiredDate = s.RequiredDate,
UnitPrice = p.UnitPrice
};
GridView2.DataSource = Controller.OrderController.GetOrderID(oid);
GridView2.DataBind();
}
以上是我写的普通版,先要求用MVC编写,顾在原cs文件中保留
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int index = gvrow.RowIndex;
string oid = GridView1.Rows[index].Cells[1].Text.Trim();
GridView2.DataSource = Controller.OrderController.GetOrderID(oid);
GridView2.DataBind();
}
并在controller下的类中添加了下面的方法
public object GetOrderID(string str)
{
NorthwindDataContext db = new NorthwindDataContext();
var orders = from s in db.Order
from p in db.Order_Detail
where s.CustomerID == str
&& s.OrderID == p.OrderID
select new
{
CustomerID = s.CustomerID,
OrderID = s.OrderID,
OrderDate = s.OrderDate,
RequiredDate = s.RequiredDate,
UnitPrice = p.UnitPrice
};
return orders;
}
但总是报错,有什么好的解决方法么 --------------------编程问答-------------------- 报错,这就好像断言“中文专业的博士必须精通鸟语”一样,必然报错。
谁让你使用mvc,问他。 --------------------编程问答-------------------- 这就好像认为:中文专业的博士,既然一种语言是博士水平,别的最低级的语言也必然都能应付。
绝对报错。本没有那么多能力,本来就是以删除各种服务器端处理能力作为噱头的asp.net mvc,不可能用web form的思路去理解编程。 --------------------编程问答-------------------- --------------------编程问答-------------------- 楼主那些代码,怎么都看不出哪点像mvc --------------------编程问答-------------------- mvc 已经没有viewState了, 不使用服务器控件了。mvc ,好东西。 --------------------编程问答-------------------- 看看asp.net mvc
Html.GridView
http://www.dotblogs.com.tw/lolota/archive/2010/03/22/14165.aspx
补充:.NET技术 , C#