Asp.net MVC学习日记十二(强大的MvcContrib,自动生成html元素)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值