防止 MVC 表单清除表单值
Prevent MVC Form to clear form values
所以我的问题很简单。提交我的表单后,所有表单值都被删除。
看看 Whosebug 上提出的问题,我觉得大多数人都有相反的问题,这是默认行为?
反正我的看法很简单
@using (Html.BeginForm("Index", "Default"))
{
<input type="text" name ="FirstName" />
<input type="text" name="LastName" />
<input type="submit" value="Send" />
}
这是控制器。
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; }
}
提交后我的输入字段为空。我如何在输入字段中保留值? (在现实生活中我当然有更大的形式)
再次调用视图时,再次转发数据,与第一次相同。 Return 查看(数据);其中数据属于某种类型(最好是 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;
}
}
演示访问
https://mrnams.com/
所以我的问题很简单。提交我的表单后,所有表单值都被删除。
看看 Whosebug 上提出的问题,我觉得大多数人都有相反的问题,这是默认行为?
反正我的看法很简单
@using (Html.BeginForm("Index", "Default"))
{
<input type="text" name ="FirstName" />
<input type="text" name="LastName" />
<input type="submit" value="Send" />
}
这是控制器。
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; }
}
提交后我的输入字段为空。我如何在输入字段中保留值? (在现实生活中我当然有更大的形式)
再次调用视图时,再次转发数据,与第一次相同。 Return 查看(数据);其中数据属于某种类型(最好是 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;
}
}
演示访问 https://mrnams.com/