Reputation: 212
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.
Upvotes: 0
Views: 81
Reputation: 14432
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 andreturn 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);
Upvotes: 1
Reputation: 239440
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);
Upvotes: 1