C# 防止MVC表单清除表单值
所以我的问题很简单。提交表单后,所有表单值都将被删除 看看Stackoverflow这里提出的问题,我认为大多数人都有相反的问题,那就是默认行为 无论如何,我的观点很简单 @使用Html.BeginFormIndex,默认值 { } 这是控制器C# 防止MVC表单清除表单值,c#,asp.net-mvc,forms,razor,postback,C#,Asp.net Mvc,Forms,Razor,Postback,所以我的问题很简单。提交表单后,所有表单值都将被删除 看看Stackoverflow这里提出的问题,我认为大多数人都有相反的问题,那就是默认行为 无论如何,我的观点很简单 @使用Html.BeginFormIndex,默认值 { } 这是控制器 public class DefaultController : Controller { // GET: Default public ActionResult Index() { var model = n
public class DefaultController : Controller
{
// GET: Default
public ActionResult Index()
{
var model = new FormViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(FormViewModel model)
{
return View(model);
}
}
我的视图模型
public class FormViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
提交后,我的输入字段为空。如何在输入字段中保留值?在现实生活中,我有一个更大的形式,当然,当再次调用视图时,再次转发数据,与第一次一样。返回视图数据;其中,数据属于某种类型,最好是ViewModel,并且由数据绑定预期的已发布表单数据填充 这是经典:
public ActionResult New ()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult New (NewPlaceVM vm)
{
if (ModelState.IsValid && vm != null)
{
var model = Mapper.Map<NewPlaceVM, Place>(vm);
_place.New(model);
return RedirectToAction("Index");
}
return View(vm);
}
在视图中,请使用EditorFor或任何其他最适合的HTML帮助程序
@model NewPlaceVM
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Name, null, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Ignore, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Ignore, null, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Ignore, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10 text-right">
<input type="submit" value="@Ress.Ress.Create" class="btn btn-primary" />
</div>
</div>
</div>
}
使用HtmlHelpers文本框
Html.TextBox("FirstName", Model.FirstName, new {
@class = "form-control",
PlaceHolder = "FirstName"
})
创建一个模型,将数据抓取到控制器中,并在渲染视图时传递模型
I have prevented form clearing for search form for my website mrnams.com
View
@using (@Html.BeginForm("Search", "Home", FormMethod.Post, new { @class = "navbar-form navbar-right pull-right" }))
{
<div class="input-group">
<input id="input-searchQuery" type="text" class="form-control" placeholder="Search this site" name="q">
<span class="input-group-btn">
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
}
jQuery functions in View
@section scripts{
<script type="text/javascript">
$(function ()
{
var queryReturned = '@ViewBag.SearchQuery';
$("#input-searchQuery").val(queryReturned);
});
</script>
}
And here is the controller.
public class Home : Controller
{
public ActionResult Search(string q)
{
ViewBag.SearchQuery = q;
}
}
进行演示访问
使用Ajax.BeginForm而不是Html。beginformdoe不会更改任何内容。还是一样的行为。@Tushar Gupta我没有看到视图问题添加@Html.TextBoxFormodel=>model.LastName;帮助。我希望我没有太多这样做,但似乎没有办法。你可以这样做会做sam的事情,但使用HTML助手更聪明