How to force Razor to make Editorfor to input number type for float variable?

前端 未结 2 912
北海茫月
北海茫月 2020-12-13 12:40

Here is my code in MVC 5:

@Html.EditorFor(model => model.myfloatvalue, new { @type = \"number\", @min = \"0\", @step = \"0.01\", @value = \"0\" })
         


        
2条回答
  •  伪装坚强ぢ
    2020-12-13 13:38

    Have you tried wrapping your anonymous object in the htmlAttributes of another anonymous object? When using EditorFor/TextBoxFor, I believe in MVC 5 that's the only way of affecting the HTML attributes output by the editor.

    @Html.EditorFor(model => model.myfloatvalue, new { htmlAttributes = new { @type = "number", @min = "0", @step = "0.01", @value = "0" }})
    

    If you not using MVC-5.1 or higher, then you will need to use TextBoxFor(). Note no htmlAttributes used here:

    @Html.TextBoxFor(m => m.myfloatvalue, new { type = "number", min = "0", step = "0.01" }) // don't set the value attribute
    

提交回复
热议问题