In the view page ( cshtml ) I would like to access the entity such as products detail, price or something in my case. First, for products model, it includes following properties :
public int ProductId { get; set; }
public int? CatId { get; set; }
public string ProductSdesc { get; set; }
public string ProductLdesc { get; set; }
public string ProductImage { get; set; }
public decimal? Price { get; set; }
public bool? Instock { get; set; }
public int Inventory { get; set; }
public ProductCategories Cat { get; set; }
So I would like to access one of these properties in the view page ( cshtml)
Following is my code trying to get the property in the C# razor page.
public class ElectronicDetailsModel : PageModel
{
private readonly XYZshopping.Models.XYZEVEDBContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;
[BindProperty]
public IList<Products> Products { get; set; }
public ElectronicDetailsModel(XYZshopping.Models.XYZEVEDBContext context, IHttpContextAccessor httpContextAccessor)
{
_context = context;
_httpContextAccessor = httpContextAccessor;
}
public IActionResult OnGet()
{
//var userName = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Name).Value;
Products = _context.Products.Where(p => p.CatId == 10).ToList();
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
Products = _context.Products.Where(p => p.CatId == 10).ToList();
await _context.SaveChangesAsync();
return RedirectToPage("./TransferSToC");
}
}
What is it supposed to be like in view page to get, for example, Price or ProductSdesc..?
<div class="row">
<div class="col-md-4">
<form>
@Model.Products.... < this part I am trying to implement...> How can I access the property in the C# (controller page) ??
</form>
</div>
--edit-- To understand what I am trying to do, On browser, I want those to be visualized looking like this: Casio HandHeld Color1 TV 2.7 --> ProductSDec $45.0000 --> Price
<form method="post">
@foreach (var product in Model.Products)
{
<tr>
<td>@product.Price</td>
<td>@product.etc...</td>
</tr>
}
</form>
or
<form method="post">
@for (int i = 0; i < Model.Products.Count; i++)
{
<tr>
<td>@Html.DisplayFor(p => p.Products[i].Price)</td>
</tr>
}
</form>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.