MonoRail和WebForm很大的一個差別就是沒有使用viewstate,不能使用WebForm中的伺服器控件。
是以頁面的取值、指派都要由我們自己控制,當然MonoRail也提供了很多種頁面互動的方式
這篇筆記主要考慮頁面之間的傳取值,對于Application、Session、Cookie等不做讨論
下面列出我看到的一些方式,有遺漏的地方,歡迎補充。
注:對于中文可能亂碼的情況:
1、需要把如下的nvelocity.properties檔案放入views/ 下:
input.encoding=GB2312
output.encoding=GB2312
2、在web.config中指定中文編碼:
<globalization requestEncoding="GB2312" responseEncoding="GB2312"/>
取值方式:

測試Html檔案
1、 最原始的Request方式:
public void Index()
{
string id = Request.Form.Get("id");
string name = Request.Form.Get("name");
string age = Request.Form.Get("age");
string birthday = Request.Form.Get("birthday");
}
2、 智能綁定方式
我們的controller需要從SmartDispatcherController繼承:
public class ServletController : SmartDispatcherController
然後就可以直接定義方法:
public void Index(int id, string name, int age, DateTime birthday)
就會自動将值作為參數綁定
另外,如果我們有如下的一個類:
public class User1
{
private int _id;
public int Id
get { return _id; }
set { _id = value; }
private string _name;
public string Name
get { return _name; }
set { _name = value; }
private int _age;
public int Age
get { return _age; }
set { _age = value; }
private DateTime _birthday;
public DateTime Birthday
get { return _birthday; }
set { _birthday = value; }
}
在綁定時可以直接綁定到這個類上面去:
public void Index([DataBind("user")]User1 user)
同時開始時的測試html也要做修改:
<html>
<head>
<title>測試頁面</title>
</head>
<body>
<form action="/index.rails" method="post" >
ID:<input type="text" name="user.id" value="1"/><br />
姓名:<input type="text" name="user.name" value="姓名"/><br />
年齡:<input type="text" name="user.age" value="22"/><br />
生日:<input type="text" name="user.birthday" value="2007-1-1"/><br />
<input id="Submit1" type="submit" value="submit" />
</form>
</body>
</html>
加了一個字首user.
另外User1類中的屬性名是不區分大小寫的,還有當輸入的日期格式不合法時,會賦預設值0001-1-1
3、Params方式
string aa = Params["id"];
string aa = Params["name"];
Params屬性中不光儲存有頁面的值,還有Http頭資訊的,比如Params["REMOTE_HOST"]就可以取得遠端主機的名稱
将值傳入頁面
1、直接使用Request對象
使用Request對象可以直接取得上一個頁面輸入的值,在vm中顯示
vm中:
$Request.Form.Get("birthday")
2、使用Flash對象
Flash對象的使用和下面的PropertyBag對象基本相同
最大的一個差別就是Flash對象是把值暫存到一個請求中的,在下一個請求中可以獲得,參見如下代碼:
PropertyBag.Add("test2", "G");
Flash.Add("test", "Spring");
RedirectToAction("Flash1");
public void Flash1()
RenderView("display");
也就是把值分别放入PropertyBag和Flash中後跳轉到下一個action
display.vm中:
$test<br />
$test2<br />
頁面顯示時能正确顯示test的值,test2值取不到
也就是說Flash對象可以儲存比如出錯資訊等,在下一個頁面中顯示
3、普通的PropertyBag方式
PropertyBag.Add("name", "姓名");
PropertyBag.Add("List", new String[] { "1", "2", "3" });
姓名:$name<br />
#foreach ($element in $list)
$element<br />
#end
可以傳入一些标準對象,對象和數組
4、使用PropertyBag将自定義對象傳入頁面
PropertyBag.Add("user", user);
姓名:$user.name<br />
對于複合情況,比如ArrayList中儲存User對象的情況也很友善:
ArrayList list = new ArrayList();
list.Add(user);
PropertyBag.Add("list", list);
$element.name<br />
5、使用Helpers對象
對于需要在vm中調用類方法的情況,比如ServletController類中有如下方法需要在vm中被調用:
public string Welcome(string str)
return "Welcome :" + str;
Index方法:
Helpers.Add("Welcome", new ServletController());
主要是要把對象放入Helpers對象中去
$Welcome.Welcome($element.name)<br />
另外如果這個方法在很多action中都要調用到,我們可以加入如下的類屬性:
[Helper(typeof(ServletController), "Welcome")]
public class ServletController : SmartDispatcherController
如果這個方法不光是這個controller中用到,我們還可以定義一個包含此屬性的父類,其他需要用到的地方直接從這個父類繼承,那麼在vm中就可以直接使用了
另,MonoRail也提供了一些标準的Helpers:
AbstractHelper[] builtInHelpers =
new AbstractHelper[]
{
new AjaxHelper(), new BehaviourHelper(),
new UrlHelper(), new TextHelper(),
new EffectsFatHelper(), new ScriptaculousHelper(),
new DateFormatHelper(), new HtmlHelper(),
new ValidationHelper(), new DictHelper(),
new PaginationHelper(), new FormHelper(),
new ZebdaHelper()
};
本文轉自永春部落格園部落格,原文連結:http://www.cnblogs.com/firstyi/archive/2007/10/23/933193.html,如需轉載請自行聯系原作者