Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display empty textbox using Html.TextBoxFor on a not-null property in an EF entity

I am using Entity Framework (v4) entities. I have an entity called Car with a Year property of type integer. The Year property does not allow NULL. I have the following in my Create view:

<%= Html.TextBoxFor(model => model.Year) %>

I am required to return a new Car object (due to other requirements) in my HttpGet Create action in the CarController.

Currently, a zero is displayed in the Year textbox because the Year property does not allow NULL. I would like to display an empty textbox in the Create view. How do I do this?

like image 779
thd Avatar asked Apr 24 '10 09:04

thd


2 Answers

Use Html Attributes Overload. In razor it would be:

@Html.TextBoxFor(model => model.Year, new { Value = "" })
like image 167
nabeelfarid Avatar answered Oct 16 '22 16:10

nabeelfarid


Try this instead:

Html.TextBox("Year", "")
like image 38
Cristi Todoran Avatar answered Oct 16 '22 16:10

Cristi Todoran