Chase Florell
Chase Florell

Reputation: 47417

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.TextBoxFor(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?

Upvotes: 1

Views: 939

Answers (2)

arena-ru
arena-ru

Reputation: 1020

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... }

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039248

You could use a Nullable(Of Double) type or double? in C#.

Upvotes: 3

Related Questions