2011-05-30 110 views
0

如何将默认值传递给模型绑定的输入?它不工作模型绑定和默认值

@Html.TextBoxFor(model => model.City, new { @class="ctype", value="default city"}) 

回答

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