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

asp.net-mvc-2 entity-framework

Question

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?

Popular Answer

Use Html Attributes Overload. In razor it would be:

@Html.TextBoxFor(model => model.Year, new { Value = "" })


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why