且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

保存后显示在同一页面

更新时间:2023-02-20 12:36:41

如果你想做到这一点,你需要清除一切的在ModelState中。结合他们的价值观,否则,当HTML辅助会完全忽略从ModelState中的模型和使用的数据。

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.

这样的:

[HttpPost]
public ActionResult Save(TestingModel model)
{
    //Save data to DB here ...          

    ModelState.Clear();
    TestingModel testingModel = new TestingModel() { FirstName = string.Empty };
    return View("Index", testingModel);
}

或只是重定向到指数 GET 在成功的情况下采取行动:

or simply redirect to the Index GET action in case of success:

[HttpPost]
public ActionResult Save(TestingModel model)
{
    //Save data to DB here ...          
    return RedirectToAction("Index");
}