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,这个是不是更赞