发布后显示状态,并保持在MVC视图中输入的模型不变

问题描述 投票:0回答:1

我有这个mvc控制器,它将一个客户添加到名为CustomerController的数据库中。该控制器具有一个称为Add的ActionResult。它按原样工作,但是我想在用户单击“提交”后显示状态消息,并且我希望添加到模型的所有信息都保留在页面上。如何将所有输入的文本保留在表单字段中,并在发布表单后显示状态消息?

    public ActionResult Add()
    {
        // This is the empty view the user see when he is about to add new form data
        return View(new CreateSupplierViewModel());
    }

    public ActionResult AddNew(CreateSupplierViewModel model)
    {
        // I post to this and need to display the status of this on the view with the entered text fields as is
        return RedirectToAction("Add", "Supplier");
    }
post model-view-controller status
1个回答
0
投票

您需要按以下方式重构代码:

CustomerController:

public ActionResult Add()
{
    return View(new CreateSupplierViewModel());
}
public ActionResult Add(CreateSupplierViewModel model)
{
    return View(model);
}

public ActionResult AddNew(CreateSupplierViewModel model)
{
    return RedirectToAction("Add", "Supplier", model);
}

您的SupplierController

public ActionResult Add(CreateSupplierViewModel model)
{

    //save the entity


    Viewbag.Message ="submit result";
    return RedirectToAction("Add", "Customer", model);
}

The Customer / Add.cshtml

@if( Viewbag.Message != null)
 {
     <p> Viewbag.Message </p>

 }
© www.soinside.com 2019 - 2024. All rights reserved.