How to set a default value with Html.TextBoxFor?

后端 未结 12 2295
清歌不尽
清歌不尽 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: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");
    }
    

提交回复
热议问题