代码之家  ›  专栏  ›  技术社区  ›  Chase Florell

asp.net mvc-当值必须是数字时如何重写默认输入值

  •  1
  • Chase Florell  · 技术社区  · 14 年前

    我得到一个 Html.TextBoxFor 用来表示 Double . 当我生成“Create”视图时

    @Html.TextBoxFor(Function(model) model.Longitude)
    

    这个 <input> 默认值为“0”

    我试过用两种方法修改它

    @Html.TextBoxFor(Function(model) model.Longitude, New With {.value = ""})
    

    在控制器里

    Dim model As Domain.Event = New Domain.Event
    With model
        .Longitude = String.Empty
    End With
    
    Return View(model)
    

    但这些都不管用。

    如何使数字输入为“空白”?

    2 回复  |  直到 14 年前
        1
  •  3
  •   Darin Dimitrov    14 年前

    你可以用一个 Nullable(Of Double) 类型或 double? 在C#中。

        2
  •  0
  •   Community CDub    7 年前

    看答案 this question

    它们覆盖模型绑定中的默认值。

    您可以从 DefaultModelBinder . 可以根据需要添加任意多的其他绑定器。

    例子: 在global.asax中注册您的活页夹 以这种方式

    protected void Application_Start() { ... your code ... ModelBinders.Binders.DefaultBinder = new YourModelBinder(); .... your code... }