1、http://mvccontrib.codeplex.com/下載MvcContrib,http://blog.jqueryui.com/2010/09/jquery-ui-1-8-5/下載jquery-ui.js。(留意版本的問題)
當然NBuilder.dll也是必須的(如果你不知道這是什麼,看看前面的,或者google下,呵呵)
2、把MvcContrib中的InputBuilderTemplates檔案夾下所有aspx頁面拖到自己項目Views->Shared下面
3、建立類ExampleModel和枚舉類ExampleTypes
public class ExampleModel
{
public Guid Key { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[Label("What type of example is this?")]
public ExampleTypes ExampleType { get; set; }
[Label("Please enter your birthday")]
[Example("mm/dd/yyyy")]
public DateTime BirthDate { get; set; }
[DataType(DataType.MultilineText)]
public string Biography { get; set; }
}
public enum ExampleTypes
{
Man = 1,
Woman = 2,
Boy = 3,
Girl = 4,
Baby = 5
}
4、在HomeController中添加
public ActionResult ShowExample()
{
ExampleModel model = Builder<ExampleModel>
.CreateNew()
.Build();
return View(model);
}
5、在Web.Config中添加
......
<add namespace="MvcContrib.UI" />
<add namespace="MvcContrib.UI.InputBuilder.Views" />
</namespaces>
</pages>
6、別忘了在Site.Master中添加js引用
<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui.js" type="text/javascript"></script>
7、添加ShowExample的強型別視圖
<fieldset>
<legend>Fields</legend>
<% Html.BeginForm(); %>
<%= Html.Input(m=>m.Key) %>
<%= Html.Input(m=>m.FirstName) %>
<%= Html.Input(m=>m.LastName) %>
<%= Html.Input(m=>m.ExampleType) %>
<%= Html.Input(m=>m.BirthDate) %>
<%= Html.Input(m=>m.Biography) %>
<div style="clear:both;">
<input type="submit" value="Submit" /></div>
<% Html.EndForm(); %>
</fieldset>
這裡已經超簡單了,不用寫過多的代碼,不是嗎?嘗過asp.net mvc這種開源的資源真是多啊,看來國外高手們都是喜歡mvc的。
在看看更簡潔的:
添加New的Action和View
public ActionResult New()
{
return View(new ExampleModel());
}
NewView中加上
<%= Html.InputForm() %>
就ok,這個是不是更贊