Asp.net MVC學習日記十二(強大的MvcContrib,自動產生html元素)

來源:互聯網
上載者:User

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,這個是不是更贊

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.