asp.net mvc:model Binding to Simple Types, Complex Types, collections, dictionaries, ETC
Environment:
Windows 2008, VS 2008 SP1, asp.net Mvc RC1
1. Simple Type
Here, we refer to the following book class as a simple type:
public class book
{public
int BookID {get; set;}
public string BookName {get; set;}
public string Author {get; set;}
Public DateTime publisheddate {get; set;}
}
Assuming that you now need to implement the Add book feature, the following action is defined in Bookcontroller:
[Acceptverbs (Httpverbs.post)]
Public ActionResult Create (book book) {
//to does
//insert book into Database
Return redirecttoaction ("Index");
}
The question now is how to name the textbox in view to achieve automatic binding, as follows:
<div>
<%using (Html.BeginForm ("Create", "book")} {%>
<div>
Book Name: <%=html.textbox ("BookName")%>
</div>
<div>
Author: <%= Html.textbox ("Author")%>
</div>
<div>
published Date: <%=html.te Xtbox ("Publisheddate")%>
</div>
<div>
<input type= "submit" ID = "Submit" Name= "submit" value= "Submit"/>
</div>
<%}%>
</div>