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 productId). Afterwards, 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 class="header">
<h1>Product Creation</h1>
</div>
<div class="row">
<form method ="post">
<div class="column side"></div>
<div class= "column middle">
<div class="ForForm">
<label asp-for=@Model.Product.Name></label>
<input asp-for=@Model.Product.Name class="form-contorl"/>
<span class="text-danger" asp-validation-for=@Model.Product.Name></span>
</div>
<div class="ForForm">
<label asp-for=@Model.Product.Version></label>
<input asp-for=@Model.Product.Version class="form-contorl"/>
<span class="text-danger" asp-validation-for=@Model.Product.Version></span>
</div>
<div class="ForForm" >
<label asp-for=@Model.Product.Amount></label>
<input asp-for=@Model.Product.Amount class="form-contorl"/>
<span class="text-danger" asp-validation-for=@Model.Product.Amount></span>
</div>
<div>
<button type="submit" class= "button button3" asp-page="/ProductReDirect" asp-route-productId=@Model.productId>Create</button>
</div>
</div>
<div class="column side"></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>
code from MVC interview questions and answers.