Forum Discussion
Bioku
Jul 09, 2023Copper Contributor
Capturing data of 1 page in another page using razor view and dotnet core
Hello Everyone, I am trying to build a mentorship software in dotnet core and I am having this problem. What I intend to do is that, when a mentee wants to register on the platform they put their...
AddWebSolution
Sep 29, 2023Brass Contributor
Hi Bioku,
To achieve this, you can use TempData in ASP.NET Core to temporarily store and pass data between actions.
1) First Page (Input Page)
In your Razor view, make sure you have a form that collects the Name, Email, and Career.
When the form is submitted, it will POST to an action in your controller.
<form method="post" asp-action="RegisterMentee">
<input type="text" name="Name" />
<input type="text" name="Email" />
<select name="Career">
<!-- Populate options from your database or enum -->
</select>
<input type="submit" value="Next" />
</form>
2) Controller Action for First Page
In your controller, have an action that handles the form submission. Store the data in TempData.
[HttpPost]
public IActionResult RegisterMentee(string Name, string Email, string Career)
{
TempData["Name"] = Name;
TempData["Email"] = Email;
TempData["Career"] = Career;
return RedirectToAction("ChooseMentor");
}
3) Second Page (Choose Mentor Page)
In the Razor view for the second page, display the list of mentors for the selected career.
Each mentor should have a button that allows the mentee to select them.
@foreach(var mentor in mentorsForSelectedCareer)
{
<div>
<span>@mentor.Name</span>
<form method="post" asp-action="SelectMentor">
<input type="hidden" name="MentorId" value="@mentor.Id" />
<input type="submit" value="Select Mentor" />
</form>
</div>
}
4)Controller Action for Second Page
In the controller, handle the mentor selection and retrieve the data from TempData to associate with the mentee.
[HttpPost]
public IActionResult SelectMentor(int MentorId)
{
// Retrieve data from TempData
string Name = TempData["Name"] as string;
string Email = TempData["Email"] as string;
string Career = TempData["Career"] as string;
// Create and save the mentee record
Mentee newMentee = new Mentee
{
Name = Name,
Email = Email,
Career = Career,
MentorId = MentorId
};
// Save the mentee to the database
return RedirectToAction("SuccessPage");
}
Note that in this step, you should replace Mentee with the actual model class you're using and adjust the code to interact with your database.
This way, you're using TempData to temporarily store the data between the two actions.
When the mentee selects a mentor, you can retrieve the data from TempData and associate it with the mentee before saving them to the database.
Best Regards,
AddWebSolution