如何设置Html.TextBoxFor默认值? [英] How to set a default value with Html.TextBoxFor?

查看:180
本文介绍了如何设置Html.TextBoxFor默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题,如果从ASP.NET MVC框架1使用HTML辅助很容易设置的默认值上一个文本框,因为有一个过载 Html.TextBox(字符串名称,对象的值)。当我尝试使用的 Html.TextBoxFor 的方法,我的第一个猜测是尝试,没有工作的情况如下:

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(string name, object value). When I tried using the Html.TextBoxFor method, my first guess was to try the following which did not work:

<%: Html.TextBoxFor(x => x.Age, new { value = "0"}) %>

我应该只是Html.TextBox(字符串对象)坚持现在?

Should I just stick with Html.TextBox(string, object) for now?

推荐答案

事实证明,如果不指定型号到控制器内的视图的方法,它不会为您创建一个对象,使用默认值

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");
}

这篇关于如何设置Html.TextBoxFor默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆