views:

129

answers:

2

Hi

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.ProductName)

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?

A: 

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" })%>
Raj Kaimal
I want to display null when the page loads up for all fields regardless of what data type it is.
chobo2
A: 

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.

Francisco Noriega
Thats what I sort of came up with. I used a field for all the ids but I never thought of using a nullable feild though. But ya pretty much all the textboxes I want to have blank default values regardless of data type.
chobo2