开发者

Possible to have empty values with Html For Helpers such as Html.TextBoxFor()?

Is it possible to have to make a html for helper in asp.net mvc 2.0 have a default value of "empty string"

Like if I do this

Html.TextBoxFor( u => u.Produc开发者_如何学PythontName)

would render to

<input id ="ProductName" name="ProdcutName" type="text" value="Jim" />

Now I don't want the textbox to display jim. I want it to display nothing.

<input id ="ProductName" name="ProdcutName" type="text" value="" />

I tried to do this

 Html.TextBoxFor( u => u.ProductName, new { @value = " "})

but that seems to do nothing. So how can I do this. I hope you can do something like this otherwise I think these new helpers have a great flaw since now I need to use like javascript to remove them since I hardly ever want a default value in the textbox especially when I have a label right beside the textbox saying what it is.

Edit

I found that if I want to set it to a null string I have to do that in the viewmodel. What is fine but still one problem is left. In my example ProductName is a string type. But if it was say a int then it would show a default value to 0 which is something I don't want.

So you can of course set a int to a empty string. So to get it to a empty string you need to change that type to a string what kinda sucks.

Any one know away around this?

Edit 2

Also the radio button for helper does not seem to generate a "id". How do you set one?


For the non nullable types you could use something like

  • int?
  • nullable

then you could set it to null.

Is it just one property you want to set to null or all of them?

If you want all of the property to be blank, it would be better to just create a new ViewModel, and then just specify the Id, while leaving the rest empty/null.


Are you wanting to display null regardless of what the value is? I do not understand.

For int, use a nullable int - int?

For the radio button use

<%= Html.RadioButton("Hello", true, new { id = "rbHello" })%>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜