2

When adding a new entry into the db, I would like to grab an ID value to be entered from a previous page. I have no problem passing the ID from initial page into the controller create method which displays the view of the submission form. But I am not sure how I can access it after the form is submitted.

I have tried using TempData, ViewData and ViewBag, but I don't see the value when trying to retrieve it in the second method.

    [HttpGet]
    public IActionResult Create(int? id){

         // I see the value here
    
            Debug.WriteLine(id);
            TempData["carid"] = id;
            Debug.WriteLine(TempData["carid"]);  
    
            return View(); // displays new entry form 
        }
        
        // called after the new entry form is submitted 
        [HttpPost]  
        public async Task<IActionResult> Create(RecordsViewModel addRecord)
        {
    
          // NO value here
    
            var carId = TempData["carid"]; 
            Debug.WriteLine(carId);   
    
    
            var record = new Records()
            {
                ID = carId, // value here is null 
                Date = addRecord.Date,
                Name = addRecord.Name,
                Cost = addRecord.Cost,
            };
    
            await carRep.Records.AddAsync(record);
            await carRep.SaveChangesAsync();
            return RedirectToAction("Create");
    
        }

Any guidance is greatly appreciated, thanks!

6
  • 1
    What's your second method? After saving in Create Post do you want to pass the ID to Create(int? id) Get right? Or Opposite one? Commented Nov 20, 2023 at 1:31
  • You may know, TempData is meant to be a one-time use. After reading the value once it would be lost. Make sure that there isn't any other code in your application that might be clearing TempData. Commented Nov 20, 2023 at 1:38
  • The most workable way is to use HttpContext.Session.SetInt32("carid", id); within your first action it can perist the value and then you can access it later on like this var carId = HttpContext.Session.GetInt32("carid"); Commented Nov 20, 2023 at 1:42
  • I referred to methods in order they appear in the code, sorry if that wasn't clear. First method is IActionResult Create(int? id) where I see the value. Second method - Task<IActionResult> Create(RecordsViewModel addRecord), where I'm trying to retrieve the value. Commented Nov 20, 2023 at 1:52
  • Alright, you can try this HttpContext.Session.SetInt32("carid", (int)id); while I am trying to test your scenario and preparing answer. Commented Nov 20, 2023 at 1:58

1 Answer 1

1

I have tried using TempData, ViewData and ViewBag, but I don't see the value when trying to retrieve it in the second method.

Well, reason you were not able to retrieve the value is all are not persistant data container. Once the action been redirected, data no longer would be either in Tempdata, viewdata or viewbag these are one time only.

In order to keep the data from one action to another action even after the page redirection you can use session which is data persistant or can keep the data as long as you want.

You can do as following:

Set Value:

[HttpGet]
 public IActionResult Create(int? id)
 {

     // I see the value here

     Debug.WriteLine(id);
     //TempData["carid"] = id;
     //Just for testing
     HttpContext.Session.SetInt32("carid", (int)id!);
 
     return View(); // displays new entry form 
 }

Get Value:

[HttpPost]
public async Task<IActionResult> Create(RecordsViewModel addRecord)
{

    // NO value here

    //Just for testing
    var carId = HttpContext.Session.GetInt32("carid");

   // var carId = TempData["carid"];
    Debug.WriteLine(carId);


    //var record = new Records()
    //{
    //    ID = carId, // value here is null 
    //    Date = addRecord.Date,
    //    Name = addRecord.Name,
    //    Cost = addRecord.Cost,
    //};

    //await carRep.Records.AddAsync(record);
    //await carRep.SaveChangesAsync();
    return RedirectToAction("Create");

}

Note: In order to test the scenario I have commented few of your code. Just use it as you need.

Program.cs:

builder.Services.AddSession(options =>
{
    options.Cookie.Name = "YourCookieName";
    options.IdleTimeout = TimeSpan.FromMinutes(2);
    options.Cookie.HttpOnly = true;
    options.Cookie.IsEssential = true;
});

Middleware Order:

app.UseRouting();
app.UseSession();
app.UseAuthorization();

Output:

enter image description here

enter image description here

Note: Please refer to this official document for more implementation sample.

Sign up to request clarification or add additional context in comments.

2 Comments

This solution did work. Thank you so much for taking the time!
Glad to know that you were able to resolve your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.