如何将默认值传递给模型绑定输入?它无法正常工作
@Html.TextBoxFor(model => model.City, new { @class="ctype", value="default city"})
答案 0 :(得分:1)
您可以在渲染此视图的控制器操作中执行此操作:
public ActionResult Index()
{
SomeViewModel model = ...
model.City = "default value";
return View(model);
}
然后:
@Html.TextBoxFor(model => model.City, new { @class = "ctype" })
或者如果您想使用HTML5 placeholder属性,可以执行以下操作:
@Html.TextBoxFor(model => model.City, new { @class = "ctype", placeholder = "default value" })
或者如果你使用弱类型助手(绝对不推荐):
@Html.TextBox("City", "default value", new { @class = "ctype" })