要解決這個字符串默認爲空,而不是「」我不得不這樣做第一:
Public Class NullToEmptyStringModelBinder
Inherits DefaultModelBinder
Protected Overrides Sub SetProperty(ByVal controllerContext As System.Web.Mvc.ControllerContext, ByVal bindingContext As System.Web.Mvc.ModelBindingContext, ByVal propertyDescriptor As System.ComponentModel.PropertyDescriptor, ByVal value As Object)
If value Is Nothing AndAlso propertyDescriptor.PropertyType Is GetType(String) Then
value = ""
End If
MyBase.SetProperty(controllerContext, bindingContext, propertyDescriptor, value)
End Sub
End Class
,然後添加到應用程序啓動此:
ModelBinders.Binders.DefaultBinder = New NullToEmptyStringModelBinder
顯示偶然一些代碼? – Jimmy 2010-02-24 21:40:53
它不會顯示太多。 暗淡數p作爲新的Person P.Name = 「」 tryupdatemodel(P) 如果名稱回發項目是空白的,p.name現在是空,不爲空。而在MVC 1下它將是空白的。 – Solmead 2010-02-24 23:31:48