Urgen
Urgen

Reputation: 1223

Property field return nulls

My server/client side application has following controllers. However when I post/create a reservation, it does not the hold/cache the value of (id) property return (CreatedAtAction(nameof(GetById), new{id=order.id}, order} method, when post action goes out of scope. Therefore GetById(int id) does not receive last id generated rather shows 0. However data commits successfully in the database with valid id.

Server side controller:

    [HttpGet("{id:int}")]
            public ActionResult<OrderTable> GetById(int id)
            {
                var order = _context.ReservationsTables.FirstOrDefault(o => o.Id == id);
                if(order == null)
                {
                    return NotFound();
                }
                else
                {
                    return order;
                }
            }
            [HttpPost]
            public ActionResult<OrderTable> CreateOrder(OrderTable order)
            {           
                _context.ReservationsTables.Add(order);
                _context.SaveChanges();
                return CreatedAtAction(nameof(GetById), new { id = order.Id }, order);
            }

Client Side controller:

 public async Task<IActionResult> CreatePostAsync(OrderTable order)
            {
                var httpClient = _clientFactory.CreateClient();
                HttpResponseMessage response = await httpClient.PostAsJsonAsync("https://localhost:44387/api/Reservation", order);
                if (response.IsSuccessStatusCode)
                {
                    var orderResult = await response.Content.ReadAsAsync<OrderTable>();
                    return RedirectToAction("ThankYouAsync", new { id = order.Id });
                }
                else
                {
                    return View("An error has occurred");
                }
            }
            public async Task<IActionResult> ThankYouAsync(int orderId)
            {
                var httpClient = _clientFactory.CreateClient();
                httpClient.BaseAddress = new Uri("https://localhost:44387/");
                HttpResponseMessage response = await httpClient.GetAsync("api/Reservation/" + orderId);

                if (response.IsSuccessStatusCode)
                {
                    var orderResult = await response.Content.ReadAsAsync<OrderTable>();
                    return View(orderResult);
                }
                else
                {
                    return View("An error has occurred");
                }
            }


 [HttpGet]
        public async Task<IActionResult> Create()
        {
            await PopulateRestaurantDropDownListAsync();
            return View();

        }

Upvotes: 1

Views: 99

Answers (1)

Nkosi
Nkosi

Reputation: 247443

The redirected action has the following signature

Task<IActionResult> ThankYouAsync(int orderId)

Note that the parameter name is orderId.

Yet when the redirection is done,

return RedirectToAction("ThankYouAsync", new { id = order.Id });

the value object is defined new { id = order.Id }

The parameter names need to match for it to work as expected.

So update RedirectToAction to use the correct property name

return RedirectToAction("ThankYouAsync", new { orderId = order.Id });

Note the new { orderId = order.Id } in the value object with matching orderId parameter.

Upvotes: 2

Related Questions