保存后显示在同一页面 [英] Display the same page after saving

查看:85
本文介绍了保存后显示在同一页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想展示一些域(1中的例子),提交,保存和各个领域的复位显示在同一页面的表单。当我提交万阿英,蒋达清,我走了保存的动作,但是当我显示视图的形式仍是填写。

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.

模型:

public class TestingModel
{
    public string FirstName { get; set; }
}

控制器:

    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);
    }
}

视图:

@using (Html.BeginForm("Save", "Child",FormMethod.Post))
{
    @Html.TextBoxFor( m => m.FirstName)
   <input type="submit" id="btSave" />
}

在标识调试的观点,在Immediat窗口 Model.FirstName =但是,当页面显示我仍然有值公布。我尝试了 ReditrectionToAction(「指数」)保存方法,但相同的结果的末尾。

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 Save method but same result.

你有一个想法?

谢谢,

推荐答案

如果你想做到这一点,你需要清除一切的在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");
}

这篇关于保存后显示在同一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆