asp.net-mvc 保存后显示相同页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6856451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display the same page after saving
提问by Kris-I
I'd like show a form with some field (one in the example), submit it, save and display the same page with a reset of all fields. The probelm when I submit, I go the "Save" action but when I display the view the form is still filled in.
我想显示一个带有一些字段的表单(示例中的一个),提交它,保存并显示相同的页面,并重置所有字段。当我提交问题时,我会执行“保存”操作,但是当我显示视图时,表单仍然被填写。
The model :
该模型 :
public class TestingModel
{
public string FirstName { get; set; }
}
The controller :
控制器:
public class ChildController : Controller
{
public ActionResult Index()
{
TestingModel model = new TestingModel();
return View(model);
}
public ActionResult Save(TestingModel model)
{
Console.WriteLine(model.FirstName); //OK
//Save data to DB here ...
TestingModel testingModel = new TestingModel() { FirstName = string.Empty };
return View("Index", testingModel);
}
}
The view :
风景 :
@using (Html.BeginForm("Save", "Child",FormMethod.Post))
{
@Html.TextBoxFor( m => m.FirstName)
<input type="submit" id="btSave" />
}
When Id debug to the view, in "Immediat window" Model.FirstName = ""but when the page is show I still have the value posted. I tried a ReditrectionToAction("Index")at the end of the Savemethod but same result.
当 Id 调试到视图时,在“即时窗口”中,Model.FirstName = ""但是当页面显示时,我仍然发布了该值。我ReditrectionToAction("Index")在Save方法结束时尝试了 a但结果相同。
Do you have an idea ?
你有想法吗 ?
Thanks,
谢谢,
回答by Darin Dimitrov
If you want to do this you need to clear everything that's in the ModelState. Otherwise HTML helpers will completely ignore your model and use data from ModelState when binding their values.
如果您想这样做,您需要清除 ModelState 中的所有内容。否则 HTML 助手将完全忽略您的模型并在绑定它们的值时使用来自 ModelState 的数据。
Like this:
像这样:
[HttpPost]
public ActionResult Save(TestingModel model)
{
//Save data to DB here ...
ModelState.Clear();
TestingModel testingModel = new TestingModel() { FirstName = string.Empty };
return View("Index", testingModel);
}
or simply redirect to the Index GETaction in case of success:
或者GET在成功的情况下简单地重定向到 Index操作:
[HttpPost]
public ActionResult Save(TestingModel model)
{
//Save data to DB here ...
return RedirectToAction("Index");
}
回答by Amir Ismail
Try to return Indexview without any model
尝试在Index没有任何模型的情况下返回视图
return View("Index");
回答by simonlchilds
You should be posting your form back to the same ActionResult
你应该把你的表格发回原来的 ActionResult
public ActionResult Index()
{
TestingModel model = new TestingModel();
return View(model);
}
[HttpPost]
public ActionResult Index(TestingModel model)
{
Console.WriteLine(model.FirstName); //OK
//Save data to DB here ...
return RedirectToAction("Index");
}
You would be able to use the parameterless overload for BeginFormtoo
你将能够使用参数的过载BeginForm过
@using(Html.BeginForm())
{
//form
}

