Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
873 views
in Technique[技术] by (71.8m points)

post - 发布后显示状态并保持输入的模型与MVC视图中的状态相同(show status after post and keep entered model as is on MVC view)

I have this mvc controller that add a customer to the database called CustomerController.

(我有这个mvc控制器,它将一个客户添加到名为CustomerController的数据库中。)

This Controller has one ActionResult called Add.

(该控制器具有一个称为Add的ActionResult。)

It works as it is but I want to display a status message after the user hit submit, and I want all information added to the model be kept on the page as is.

(它按原样工作,但是我想在用户单击“提交”后显示状态消息,并且我希望添加到模型的所有信息都保留在页面上。)

How can I keep the all the entered text in the form fields and also show a status message after the form has been posted?

(如何将所有输入的文本保留在表单字段中,并在表单发布后显示状态消息?)

    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");
    }
  ask by MTplus translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to refactor your code as below :

(您需要如下重构代码:)

The CustomerController :

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

Your SupplierController

(您的SupplierController)

public ActionResult Add(CreateSupplierViewModel model)
{

    //save the entity


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

The Customer/Add.cshtml (show the submit result in view)

(Customer / Add.cshtml(在视图中显示提交结果))

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...