Asp.Net Mvc中的一些初级问题整理

1.如何让Controller不返回视图?(比如仅执行一些数据库操作)

很简单,只要在Controller中定义一个void类型的public方法即可

public void DeleteData()
{
    using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + Server.MapPath(_dbFile)))
    {
	conn.Open();
	SQLiteCommand cmd = conn.CreateCommand();
	cmd.CommandText = "delete from Products";
	cmd.ExecuteNonQuery();
    } 
    
    //顺便给个sqlite使用事务的代码
    //using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + Server.MapPath(_dbFile)))
    //{
	//conn.Open();
	//SQLiteTransaction t = conn.BeginTransaction();
	//try
	//{
	    //SQLiteCommand cmd = conn.CreateCommand();
	    //cmd.CommandText = "insert into Products(Name,CreateDate,UpdateDate) values(@Name,@CreateDate,@UpdateDate)";

	    //for (int i = 0; i < 50; i++)
	    //{
		//cmd.Parameters.Clear();
		//cmd.Parameters.AddWithValue("Name", i.ToString().PadLeft(5, '0'));
		//cmd.Parameters.AddWithValue("CreateDate", DateTime.Now);
		//cmd.Parameters.AddWithValue("UpdateDate", DateTime.Now);
		//cmd.ExecuteNonQuery();
	    //}

	    //t.Commit();
	//}
	//catch
	//{
	    //t.Rollback();
	//}
    //}
}

这样就行了,调用方法类似 : http://localhost/Product/DeleteData 即可
2.如何让视图返回纯文本或Xml?

public ActionResult GetTxt() 
{
    return new ContentResult() { ContentType = "text/plain", ContentEncoding = Encoding.UTF8, Content = "Hello World!" };
}

 如果要返回xml,把text/plain改为text/xml即可.

3.如何把DataTable传给视图?

虽然很多官方教程都是推荐使用强类型的视图,但是需求是千变万化的,如果确实要传递DataTable给视图,可参考下面这个做:

public ActionResult Index()
{
    DataTable tbl = new DataTable();
    using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + Server.MapPath(_dbFile)))
    {
	SQLiteDataAdapter da = new SQLiteDataAdapter("Select * from Products", conn);
	da.Fill(tbl);
	ViewData["data"] = tbl;
    }
    return View();
}

 然后在视图上可以这样写:

<%
    DataTable tbl = ViewData["data"] as DataTable;
    foreach (DataRow dr in tbl.Rows)
    {
	//...
    }
%>

 4.如何使用自定义控件(做为数据显示模板)?

创建一个Partial View(局部视图),内容可参考这样:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ Import Namespace="System.Data" %>
<tr>
    <td>
        <%=(ViewData.Model as DataRow)["Id"] %>
    </td>
    <td>
        <%=(ViewData.Model as DataRow)["Name"]%>
    </td>
    <td>
        <%=(ViewData.Model as DataRow)["CreateDate"]%>
    </td>
    <td>
        <%=(ViewData.Model as DataRow)["UpdateDate"]%>
    </td>
</tr>

 然后在主视图中可这样使用:

<table>
<tr>
    <th>
	Id
    </th>
    <th>
	Name
    </th>
    <th>
	CreateDate
    </th>
    <th>
	UpdateDate
    </th>
</tr>
<%
    DataTable tbl = ViewData["data"] as DataTable;
    foreach (DataRow dr in tbl.Rows)
    {
	Html.RenderPartial("~/Views/Product/ProductData.ascx", dr);
    }
%>
</table>

 5.页面/视图之间如何跳转?

分二种情况:

void类型的action(即问题1中所说的不返回视图的action):

有且只有一种方法:

Response.Redirect("/product/Index"); 

 注意:
如果写成Redirect("/product/Index"); 编译也会通过,但是根本不会有效果,因为前面不加Response.则变成了Controller类的Redirect方法,这个是有返回值的,必须用return Redirect()调用才会有效果,但是该方法又是void类型的,不允许return,所以才说这是有且仅有的一种方法.

常规返回ActionResult的action:

方法就很多了:

public ActionResult ShowView1()
{            
    Response.Redirect("ShowView2"); //方法1          
    //return Redirect("ShowView2"); //方法2
    //return RedirectToAction("ShowView2"); //方法3
    return View("ShowView2"); //方法4
}

public ActionResult ShowView2()
{
    ViewData["data"] = "View2";
    return View();
}
posted @ 2010-03-06 17:13  菩提树下的杨过  阅读(1029)  评论(2编辑  收藏  举报