熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

DefaultModelBinder實現各種類型及字典類型的自動綁定

2013-11-13 09:53:31  來源: .NET編程 

  本文主要實驗如何應用AspNet Mvc內建功能(DefaultModelBinder)實現簡單類型復雜類型集合類型以及字典類型的自動綁定

   簡單類型
這裡我們將下面這個Book類稱為簡單類型

  public class Book
    {
        public int BookId { get; set; }
        public string BookName { get; set; }
        public string Author { get; set; }
        public DateTime PublishedDate { get; set; }
    }

  假設現在需要實現添加Book的功能那麼在BookController中會定義如下的Action

  [AcceptVerbs(HttpVerbsPost)]
        public ActionResult Create(Book book) {
            //TO DO
            //Insert book into Database
            return RedirectToAction(Index);
        }
現在的問題便是在View中如何命名TextBox來達到自動綁定如下

  <div>
        <%using (HtmlBeginForm(Create Book)) { %>
        <div>
            Book Name: <%=HtmlTextBox(BookName)%>
        </div>
        <div>
            Author: <%=HtmlTextBox(Author)%>
        </div>
        <div>
            Published Date: <%=HtmlTextBox(PublishedDate)%>
        </div>
        <div>
            <input type=submit id=submit name=submit value=submit  >


From:http://tw.wingwit.com/Article/program/net/201311/11878.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.