如何在mvc5中向视图添加空文本框 [英] how to add empty textbox to a view in mvc5
本文介绍了如何在mvc5中向视图添加空文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@Html.TextBoxFor(m=> s.MyName, new { htmlAttributes = new { @class = "form-control" } })
MyName属性在db中有一个值TestUser。
当我使用上面的行时,它会显示一个文本框,但它不是空的,它有一个值TestUser。如何显示空文本框? (考虑前面的注册表格)在mvc 5
MyName property has a value "TestUser" in db.
When i use the above line it displays a textbox but it is not empty, it has a value "TestUser". How to display an empty textbox? (considering an ex of a registration form) in mvc 5
推荐答案
您发送给视图的模型不为空,因此它不会为空。如果你要注册表,请考虑这样的事情。
The model you send to the view is not empty and therefore it will not be empty. If you will make an registration form consider something like this.
[HttpGet]
public ActionResult Register()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
// Register user logic
return RedirectToAction("Index", "Home");
}
// If we got this far, something failed, redisplay form
return View(model);
}
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new account.</h4>
@Html.ValidationSummary()
<div class="form-group">
@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
</div>
</div>
<!-- More inputs or something -->
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Register" />
</div>
</div>
}
这篇关于如何在mvc5中向视图添加空文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文