为什么 DefaultModelBinder 不绑定到值为“”的 Char 属性”c
我有一个像在我的控制器中的类
Public Class Task
Property DuplexType As Char
Property Name As String
End Class
,我有一个看起来像
<HttpPost()>
Function Edit(ByVal task As Task) As ActionResult
Dim duplexType = task.DuplexType
Dim valid = ModelState.IsValid
Return RedirectToAction("Index")
End Function
在视图中的操作,DuplexType =“”(单个空格)和Name =“Foo”。为什么属性 DuplexType 没有值?如果我分配任何其他角色,它就可以正常工作。 在控制器中 name = "foo" 但 DuplexType = " (空)。
如果 DuplexType = " ",则 ModelState.IsValid = false。
I have a class like
Public Class Task
Property DuplexType As Char
Property Name As String
End Class
In my controller I have an action that looks like
<HttpPost()>
Function Edit(ByVal task As Task) As ActionResult
Dim duplexType = task.DuplexType
Dim valid = ModelState.IsValid
Return RedirectToAction("Index")
End Function
In the view, DuplexType = " " (single space) and Name = "Foo". Why doesn't the property DuplexType have a value? If I assign any other character it works fine.
In the controller name = "foo" but DuplexType = " (empty).
Also ModelState.IsValid = false if DuplexType = " ".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 Response 对象并检查由 Post 表单填充的值。您可能会看到值被修剪,因此失去了空间。如果可能,请尝试对值进行 UrlEncode。
当我必须在自己的项目中保留尾随空格时,我最终不得不在参数上放置分隔符,然后在操作方法中将它们去掉。
Have a look at the Response object and examine the values being populated by the form Post. You will probably see that values are trimmed and therefore lose the space. If possible try to UrlEncode the values.
Where I have had to preserve trailing spaces in my own projects, I have ended up having to put delimiters on the parameter and then strip them off in the action method.