Forum Discussion

Bioku's avatar
Bioku
Copper Contributor
Jul 09, 2023

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 Name, email and choose from list of career (career being generated as a drop down from another table from the db or can also be generated as drop down from enum). So, when mentee clicks next, the system routes them to another page where they can choose mentor based on the career they have chosen and then when they choose a mentor, they should have a button to post the information from previous page (Name and email ) and the Id of the selected mentor in Page 2. 

 

The problem I am having is that the data from the first page(The page where a new mentee inputs their email and name) is lost when it routes to the second page(the page to choose a mentor).

 

So, I want a way to capture the data in the first page so that I can post these data alongside the data in the second page to the database (Mentee Table)

 

Mentor is a foreign key in the mentee table because the schema is designed so that a mentee can only have 1 mentor but 1 mentor can have multiple mentees.

1 Reply


  • 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

Resources