Hi
Scott has said that:"Preview 5" doesn't have a built-in
[Bind] attribute like above just yet (although we are considering
adding it as a built-in feature of ASP.NET MVC in the future). However
all of the framework infrastructure necessary to implement a [Bind]
attribute like above is now implemented in preview 5.
The open source MVCContrib project also has a DataBind attribute like
above that you can use today.
So you can not use the Bind attribute in this version(Maybe the next version,and the version maybe a first beta version), But that dosn't means that you could not use ModelBinder in this version,If you wanna use ModelBinder,You need to do the following steps.
1,Write your own ModelBinder,Here is Mine(Thanks to chsword)
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Globalization;
5 using System.Linq;
6 using System.Web.Mvc;
7
8 namespace CMS.Models
9 {
10 public class CMSModelBinder:IModelBinder
11 {
12 #region IModelBinder Members
13
14 public object GetValue(ControllerContext controllerContext, string modelName, Type modelType, ModelStateDictionary modelState)
15 {
16 //Instant Model Type
17 object model = Activator.CreateInstance(modelType);
18 //Get the Object property collections
19 IEnumerable<string> keys = modelType.GetProperties().Select(c => c.Name);
20 //Object Prefix,if the passing modelName is content,Will check name="content.ID" name="content.title"
21 string objectPrefix = modelName;
22 //Property Collections
23 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(model);
24 var dictionary = new Dictionary<string, PropertyDescriptor>();
25
26 foreach (string str in keys)
27 {
28 if (!string.IsNullOrEmpty(str))
29 {
30 PropertyDescriptor descriptor = properties.Find(str, true);
31 if (descriptor == null)
32 {
33 throw new ArgumentException(
34 string.Format(CultureInfo.CurrentCulture,"Property dosn't exists:{0},{1}",new object[]{model.GetType().FullName,str}),"modelName");
35
36 }
37 //Link the object and property names
38 string str3 = string.IsNullOrEmpty(objectPrefix) ? str : (objectPrefix + "." + str);
39 dictionary[str3] = descriptor;
40 }
41 }
42 foreach (var pair in dictionary)
43 {
44 string key = pair.Key;
45 PropertyDescriptor descriptor2 = pair.Value;
46 object obj2 = ModelBinders.GetBinder(descriptor2.PropertyType).GetValue(controllerContext, key, descriptor2.PropertyType, modelState);
47 if (obj2!=null)
48 {
49 try
50 {
51 //Set the Property Value
52 descriptor2.SetValue(model, obj2);
53 continue;
54 }
55 catch
56 {
57 //If we have a Vaildation Helper,It will shown in Html.ValidationSummary中
58 string errorMessage = string.Format(CultureInfo.CurrentCulture, "Validation failed{0}:{1}", new[] { obj2, descriptor2.Name });
59 string attempedValue = Convert.ToString(obj2, CultureInfo.CurrentCulture);
60 modelState.AddModelError(key, attempedValue, errorMessage);
61 continue;
62 }
63 }
64 }
65 //At last return the object
66 return model;
67 }
68
69 #endregion
70 }
71 }
2) you need to register ModelBinders
1 [AcceptVerbs("POST")]
2 public ActionResult Create([ModelBinder(typeof(CMSModelBinder))]ArticleAdd article)
3 {}
3) The View Page
<span class="message">
<%= ViewData["Message"]%>
</span>
<%= Html.ValidationSummary() %>
<% using (Html.Form("Article","Create",FormMethod.Post)) { %>
<table width="98%">
<tr>
<td>Title:</td>
<td>
<%= Html.TextBox("article.title") %>
<%=Html.ValidationMessage("title","*") %>
</td>
</tr>
<tr>
<td>Alias:</td>
<td><%= Html.TextBox("article.alias") %></td>
</tr>
</table>