asp.net – Two-page form

Question:

There is a Create.cshtml page with a Create() method and a Create2.cshtml page with a Create2() method.

How to make it so that by clicking the button in Create.cshtml transferred to the Create2.cshtml page and by clicking the button in Create2.cshtml this whole thing has already been saved?

Here are the methods:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,Name")] Test_Run test_Run)
    {
        if (ModelState.IsValid)
        {
            //чо-то делается

            return View("Create2", test_Run);
        }
        return View(test_Run);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create2(Test_Run test_Run)
    {
        {
            //чо-то делается, а потом сохраняем

            db.Test_Runs.Add(test_Run);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(test_Run);
    }

The buttons in both views are of submit type, but when I poke the button in the second view, the first one is validated, and the Create2() method does not reach at all

Answer:

I think the problem is in what is written in Create2 View, namely the Html.BeginForm () element. Most likely the path to Create View is specified there.

You need to write something like this

@using (Html.BeginForm("Create2", "Home",
    new { test_Run = Model },
    FormMethod.Post,
    new { @class = "userCssClass", data_formType="user" }))
{ 
    ...
}

You can read more here: Creating HTML Forms

Scroll to Top