开发者

Getting exception when trying to access FormValues() element

The exception:

"Input string was not in a correct format."

Appears when I try to access a field on my page like this:

.TransmissionId = IIf(FormValues("TransmissionId").Length > 0, Integer.Parse(FormValues("TransmissionId")), 0)

However it works for another field which is the same exact type of field, accessed like this:

.StateId = IIf(FormValues("StateId").Length > 0, Integer.Parse(FormValues("StateId")), 0)

Where could the mismatch and exception be coming from? What could be the fault? The field doesn't have a value when I'm pulling it. Do I have t开发者_Go百科o test for a value different for this number field than I would have to for a text field?


Well obviously you are trying to parse a string value to an integer which cannot be parsed to an integer. Look at what's actually sent to the server to understand why the value cannot be parsed as integer. FireBug is a useful tool for this. Especially look for the TransmissionId value in the request (is it present, what's the value). You could also use the TryParse method to avoid exceptions and handle the error gracefully.

A better solution would be to use view models as action arguments and leave the parsing to the default model binder. This way you could completely get rid of the code you've written as it would be handled by the binder.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜