admin管理员组

文章数量:1291109

I use Html helpers to display model data in form fields, e.g. Html.TextBoxFor, Html.TextAreaFor.

When the model values are null, I would expect the values should be empty in the form fields. They are displayed normally in Safari and Firefox, i.e. empty, but in IE, they are shown as "null" in a text field (see image below).

Any clue how to fix this? Thanks.

I use Html helpers to display model data in form fields, e.g. Html.TextBoxFor, Html.TextAreaFor.

When the model values are null, I would expect the values should be empty in the form fields. They are displayed normally in Safari and Firefox, i.e. empty, but in IE, they are shown as "null" in a text field (see image below).

Any clue how to fix this? Thanks.

Share Improve this question edited Mar 24, 2011 at 1:29 William Niu asked Mar 22, 2011 at 5:02 William NiuWilliam Niu 15.9k8 gold badges57 silver badges93 bronze badges 2
  • What version of IE are you using? – byte Commented Mar 23, 2011 at 9:46
  • I just found out that, it's not ASP.NET MVC's problem; it's JavaScript... :p I am still investigating the cause though. – William Niu Commented Mar 24, 2011 at 0:01
Add a ment  | 

1 Answer 1

Reset to default 8

Well, it turned out to be JavaScript's problem (with IE, that is). In the following statement, if value == null, IE would display null in the textbox (or textarea).

$('#someTextBox').val(value);

The quick fix is simply display an empty string instead...

$('#someTextBox').val(value == null ? '' : value);

本文标签: javascriptquotnullquot values shown in form fields in IEStack Overflow