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

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

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

01 public void DeleteData()
02 {
03     using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + Server.MapPath(_dbFile)))
04     {
05     conn.Open();
06     SQLiteCommand cmd = conn.CreateCommand();
07     cmd.CommandText = "delete from Products";
08     cmd.ExecuteNonQuery();
09     
10       
11     //顺便给个sqlite使用事务的代码
12     //using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + Server.MapPath(_dbFile)))
13     //{
14     //conn.Open();
15     //SQLiteTransaction t = conn.BeginTransaction();
16     //try
17     //{
18         //SQLiteCommand cmd = conn.CreateCommand();
19         //cmd.CommandText = "insert into Products(Name,CreateDate,UpdateDate) values(@Name,@CreateDate,@UpdateDate)";
20   
21         //for (int i = 0; i < 50; i++)
22         //{
23         //cmd.Parameters.Clear();
24         //cmd.Parameters.AddWithValue("Name", i.ToString().PadLeft(5, '0'));
25         //cmd.Parameters.AddWithValue("CreateDate", DateTime.Now);
26         //cmd.Parameters.AddWithValue("UpdateDate", DateTime.Now);
27         //cmd.ExecuteNonQuery();
28         //}
29   
30         //t.Commit();
31     //}
32     //catch
33     //{
34         //t.Rollback();
35     //}
36     //}
37 }

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

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

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

3.如何把DataTable传给视图?

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

01 public ActionResult Index()
02 {
03     DataTable tbl = new DataTable();
04     using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + Server.MapPath(_dbFile)))
05     {
06     SQLiteDataAdapter da = new SQLiteDataAdapter("Select * from Products", conn);
07     da.Fill(tbl);
08     ViewData["data"] = tbl;
09     }
10     return View();
11 }

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

1 <%
2     DataTable tbl = ViewData["data"] as DataTable;
3     foreach (DataRow dr in tbl.Rows)
4     {
5     //...
6     }
7 %>

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

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

01 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
02 <%@ Import Namespace="System.Data" %>
03 <tr>
04     <td>
05         <%=(ViewData.Model as DataRow)["Id"] %>
06     </td>
07     <td>
08         <%=(ViewData.Model as DataRow)["Name"]%>
09     </td>
10     <td>
11         <%=(ViewData.Model as DataRow)["CreateDate"]%>
12     </td>
13     <td>
14         <%=(ViewData.Model as DataRow)["UpdateDate"]%>
15     </td>
16 </tr>

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

01 <table>
02 <tr>
03     <th>
04     Id
05     </th>
06     <th>
07     Name
08     </th>
09     <th>
10     CreateDate
11     </th>
12     <th>
13     UpdateDate
14     </th>
15 </tr>
16 <%
17     DataTable tbl = ViewData["data"] as DataTable;
18     foreach (DataRow dr in tbl.Rows)
19     {
20     Html.RenderPartial("~/Views/Product/ProductData.ascx", dr);
21     }
22 %>
23 </table>

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

分二种情况:

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

有且只有一种方法:

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

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

常规返回ActionResult的action:

方法就很多了:

01 public ActionResult ShowView1()
02 {            
03     Response.Redirect("ShowView2"); //方法1          
04     //return Redirect("ShowView2"); //方法2
05     //return RedirectToAction("ShowView2"); //方法3
06     return View("ShowView2"); //方法4
07 }
08   
09 public ActionResult ShowView2()
10 {
11     ViewData["data"] = "View2";
12     return View();
13 }
posted @ 2010-08-31 10:55  肚肚  阅读(196)  评论(0编辑  收藏  举报