asp.net mvc - how to override default input value when value must be a number
Ive got an Html.TextBoxFor
that is used to represent a Double
. When I generate "Create" View
@Html.TextBoxFor(Function(model) model.Longitude)
The <input>
has a default value of "0"
I have tried modifying it in two ways
@Html.TextB开发者_运维知识库oxFor(Function(model) model.Longitude, New With {.value = ""})
and in the controller
Dim model As Domain.Event = New Domain.Event
With model
.Longitude = String.Empty
End With
Return View(model)
But neither of these work.
How can I have the input "Blank" for a numeric input?
You could use a Nullable(Of Double)
type or double?
in C#.
Look at answer of this question
They overwrite default values in model binding.
You can derive your custom model binder from DefaultModelBinder. You can add as many other Binders as you need.
Example: Register your Binder in global.asax in such way
protected void Application_Start() {
... your code ...
ModelBinders.Binders.DefaultBinder = new YourModelBinder();
.... your code...
}
精彩评论