Home > Blockchain >  TempData is always 0
TempData is always 0

Time:03-06

Background regarding the web application I am writing. I have a razor page that creates a Product (the information is inserted into the database and the database is creating the product Id). Afterward, in the post request, I want to redirect to another page razor that shows all the information about that product.

I was trying to pass the product Id by using the TempData, but for some reason, it's always 0. I want to point out that I am fairly new to MVC and ASP.NET core. I understand I might need to create a controller to pass the data, but I am not sure. Below is my code:

CreateProduct.cshthml.cs:

using Accounts.Data;
using Accounts.Domain;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Template_Site.Pages
{
    public class CreateProductModel : PageModel
    {
        private readonly IProductRepository _products;
        [TempData]
        public int productId { get; set; }
        [BindProperty]
        public Product Product { get; set; }
        public CreateProductModel(IProductRepository product)
        {
            _products=product;
        }
        public IActionResult OnGet()
        {
            return Page();
        }
    
    public IActionResult OnPost()
        {
            if (ModelState.IsValid)
            {

                //_products.AddProduct(Product);
                //_products.SaveAll();
               productId = _products.RecentProduct().Id;
                return RedirectToPage("/ProductReDirect", productId);

            }
            return Page();

        }
    }
}

CreateProduct.cshthml:

@page
@model Template_Site.Pages.CreateProductModel
@{
}
<div >
    <h1>Product Creation</h1>
</div>
<div >
    <form method ="post">
     <div ></div>
     <div class= "column middle">
         <div >
         <label [email protected]></label>
         <input [email protected] />
         <span  [email protected]></span>
       </div>
         <div >
         <label  [email protected]></label>
         <input  [email protected] />
         <span  [email protected]></span>
         </div>
         <div  >
         <label [email protected]></label>
         <input [email protected] />
         <span  [email protected]></span>
         </div>
       <div>
         <button type="submit" class= "button button3" asp-page="/ProductReDirect" [email protected]>Create</button>
      </div>
      </div>
    <div ></div>
    </form>
</div>

ProductReDirect.cshtml.cs:

using Accounts.Data;
using Accounts.Domain;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Template_Site.Pages
{
    public class ProductReDirectModel : PageModel
    {
        private readonly IProductRepository _product;

        public Product Product { get; set; }
        [BindProperty(SupportsGet = true)]
        public int productId { get; set; }
        public ProductReDirectModel(IProductRepository product)
        {
            _product = product;
        }
        public void OnGet()
        {
            productId = (int)TempData["productId"];
            TempData.Keep("productId");
            Product = new Product();
            Product = _product.GetById(productId);
        }
    }
}

ProductReDirect.csthml:

@page
@model Template_Site.Pages.ProductReDirectModel
@{
}
<h2>You created a new product!</h2>

<h2> id: @Model.productId</h2>

CodePudding user response:

Firstly,you need to remove asp-page and asp-route-productId in button,or you wll not go to OnPost handler in CreateProduct.cshtml.cs:

CreateProduct.cshthml:

@page
@model Template_Site.Pages.CreateProductModel
@{
}
<div >
    <h1>Product Creation</h1>
</div>
<div >
    <form method ="post">
     <div ></div>
     <div class= "column middle">
         <div >
         <label [email protected]></label>
         <input [email protected] />
         <span  [email protected]></span>
       </div>
         <div >
         <label  [email protected]></label>
         <input  [email protected] />
         <span  [email protected]></span>
         </div>
         <div  >
         <label [email protected]></label>
         <input [email protected] />
         <span  [email protected]></span>
         </div>
       <div>
         <button type="submit" class= "button button3" >Create</button>
      </div>
      </div>
    <div ></div>
    </form>
</div>

Then you need to pass productId in OnPost handler like this:

 public IActionResult OnPost()
        {
            if (ModelState.IsValid)
            {

                //_products.AddProduct(Product);
                //_products.SaveAll();
               productId = _products.RecentProduct().Id;
                return RedirectToPage("/ProductReDirect",new { productId = productId });

            }
            return Page();

        }
    }

And remove productId = (int)TempData["productId"]; in your ProductReDirect.cshtml.cs:

public void OnGet()
        {
            Product = new Product();
            Product = _product.GetById(productId);
        }
  • Related