简体   繁体   中英

passing value to route in asp.net mvc5

my first controller:

public ActionResult Create()
{
    return View();
}

[HttpPost]
public ActionResult Create(personalInfoModel personalInfo)
{
    if (ModelState.IsValid)
    {
        TempData["key"] = personalInfo.CNIC;

        PersonalInfoViewModel pivm = new PersonalInfoViewModel();
        pivm.AddNewRecord(personalInfo);

        return RedirectToAction("Create", "Experience", new { key = personalInfo.CNIC});
    }

    return View();
}

And my 2nd controller code is:

public ActionResult Create(string key)
{
    if (filled == true)
    {
        TempData["alertMessage"] = "<script>alert('Fill it first!')</script>";
    }
    filled = true;
    return View(key);
}

[HttpPost]
public ActionResult Create(experiencesModel experiences, string key)
{
    filled = false;
    ExperiencesViewModel evm = new ExperiencesViewModel();
    evm.AddNewRecord(experiences, key);
    return View();

}

I want to pass key from 1st controller to 2nd controller in which I am facing error:

The view '42201-09007860-1' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Experience/42201-09007860-1.aspx ~/Views/Experience/42201-09007860-1.ascx ~/Views/Shared/42201-09007860-1.aspx ~/Views/Shared/42201-09007860-1.ascx ~/Views/Experience/42201-09007860-1.cshtml ~/Views/Experience/42201-09007860-1.vbhtml ~/Views/Shared/42201-09007860-1.cshtml ~/Views/Shared/42201-09007860-1.vbhtml

How can I solve this issue? I need clarification about passing value from controller to controller.

The first param to View is the name of the view to render. If you want to utilize the default view based on convention, you must pass null. The second param is where you can pass a model, which would likely be your key .

return View(null, key);

Or

return View("Create", key);

I think you don't fully understand the return View() code. Here's the difference between View() and View("ViewName") :

return View() returns the view corresponding to the action method and return View("ViewName") returns the view name specified in the current view folder.

On this line:

return View(key);

You are trying to return the view that has the name that is provided in the key parameter, which won't work of course. If you want to view the Create page, just change it to following line:

return View("Create", key);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM