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

谁说胖子不能爱 提交于 2019-11-30 11:11:33

Use Html Attributes Overload. In razor it would be:

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

Try this instead:

Html.TextBox("Year", "")

If you are using the strongly typed TextAreaFor helper, then there is no direct way to set a default value. The point of the strongly typed helper is that it binds the text area to a model property and gets the value from there. If you want a default value, then putting in the model would achieve that. You can also just switch to the non-strongly typed TextArea helper. It gives you more a bit more flexibility for cases like this:

   @{
          var defaultValue= "";
        }
    @Html.TextArea("Model.Description", defaultValue, new { Value = "added text", @class = "form-control", @placeholder = "Write a description", @rows = 5 })
Captain Caveman

Try this is you are trying to append to your field or want to modify an existing field with an empty TextBoxFor.

Html.TextBoxFor(model => model.Year, Model.Year="")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!