开发者

Why does the DefaultModelBinder not bind to Char properties with value of " "c

开发者 https://www.devze.com 2023-01-15 19:14 出处:网络
I have a class like Public Class Task Property DuplexType As Char Pro开发者_运维问答perty Name As String

I have a class like

Public Class Task
    Property DuplexType As Char
    Pro开发者_运维问答perty 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 = " ".


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号