How to set a default value with Html.TextBoxFor?

后端 未结 12 2136
清歌不尽
清歌不尽 2020-12-04 07:24

Simple question, if you use the Html Helper from ASP.NET MVC Framework 1 it is easy to set a default value on a textbox because there is an overload Html.TextBox(strin

相关标签:
12条回答
  • 2020-12-04 07:39

    The default value will be the value of your Model.Age property. That's kind of the whole point.

    0 讨论(0)
  • 2020-12-04 07:39

    Try this also, that is remove new { } and replace it with string.

    <%: Html.TextBoxFor(x => x.Age,"0") %>
    
    0 讨论(0)
  • 2020-12-04 07:41

    value="0" will set defualt value for @Html.TextBoxfor

    its case sensitive "v" should be capital

    Below is working example:

    @Html.TextBoxFor(m => m.Nights, 
        new { @min = "1", @max = "10", @type = "number", @id = "Nights", @name = "Nights", Value = "1" })
    
    0 讨论(0)
  • 2020-12-04 07:41

    this worked for me , in this way we setting the default value to empty string

    @Html.TextBoxFor(m => m.Id, new { @Value = "" })
    
    0 讨论(0)
  • 2020-12-04 07:44

    It turns out that if you don't specify the Model to the View method within your controller, it doesn't create a object for you with the default values.

    [AcceptVerbs(HttpVerbs.Get)]
    public ViewResult Create()
    {
      // Loads default values
      Instructor i = new Instructor();
      return View("Create", i);
    }
    
    [AcceptVerbs(HttpVerbs.Get)]
    public ViewResult Create()
    {
      // Does not load default values from instructor
      return View("Create");
    }
    
    0 讨论(0)
  • 2020-12-04 07:47

    This should work for MVC3 & MVC4

     @Html.TextBoxFor(m => m.Age, new { @Value = "12" }) 
    

    If you want it to be a hidden field

     @Html.TextBoxFor(m => m.Age, new { @Value = "12",@type="hidden" }) 
    
    0 讨论(0)
提交回复
热议问题