Asked by:
ASP.NET virtual fields

Question
-
User580780573 posted
Good morning all.I have just started creating web applications with ASP.NET Core 3.1 with EntityFrameworkCore and I have a difficult problem to solve.I do Code first. which means that the database and the tables are created automatically after migration as well as the views.My problem is virtual fields with EntityFrameworkCoreI have two classesProduct class and Category classA product is part of a category and therefore according to the Microsoft doc it is necessary to create virtual fieldsExample: public virtual Category category {get; set;} in the Product classAfter the migration a CategoryId field is created in the database but in the product view automatic generation this field does not appearAnyone have an idea please?Here is the code for the two classes:public class Categorie
{
[Key]
public int ID { get; set; }
[Required]
[Display(Name = "Nom de la catégorie")]
public string Nom { get; set; }
public virtual IList<Produit> Produits { get; set; }
}
public class Produit
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name ="Nom du produit")]
public string Nom { get; set; }
[Required]
[Display(Name = "Prix publique")]
public string Prinx { get; set; }
public virtual Categorie Categorie { get; set; }
}
Dans le controller produit il n'y ap as le champs categorie
Voci le controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using ProduitApp.Data;
using ProduitApp.Models;
namespace ProduitApp.Controllers
{
public class ProduitsController : Controller
{
private readonly MonDbContext _context;
public ProduitsController(MonDbContext context)
{
_context = context;
}
// GET: Produits
public async Task<IActionResult> Index()
{
return View(await _context.Produits.ToListAsync());
}
// GET: Produits/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var produit = await _context.Produits
.FirstOrDefaultAsync(m => m.Id == id);
if (produit == null)
{
return NotFound();
}
return View(produit);
}
// GET: Produits/Create
public IActionResult Create()
{
ViewBag.categories = _context.Categories.ToArray();
return View();
}
// POST: Produits/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Nom,Prix")] Produit produit)
{
if (ModelState.IsValid)
{
_context.Add(produit);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(produit);
}
// GET: Produits/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var produit = await _context.Produits.FindAsync(id);
if (produit == null)
{
return NotFound();
}
return View(produit);
}
// POST: Produits/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Nom,Prix")] Produit produit)
{
if (id != produit.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(produit);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ProduitExists(produit.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(produit);
}
// GET: Produits/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var produit = await _context.Produits
.FirstOrDefaultAsync(m => m.Id == id);
if (produit == null)
{
return NotFound();
}
return View(produit);
}
// POST: Produits/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var produit = await _context.Produits.FindAsync(id);
_context.Produits.Remove(produit);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool ProduitExists(int id)
{
return _context.Produits.Any(e => e.Id == id);
}
}
}
As you can see, in the Bind it misses the category field and even if I add it manually it still does not work
And also in the product creation view it hides the category field
This field is always null in the databaseSunday, February 9, 2020 1:48 PM
All replies
-
User1535942433 posted
Hi thomas51516,
Accroding to your description and codes,the default scaffold is product.You could bind the product and the category field is navigation properties.So the category couldn't be bound.
More details,you could refer to below article:
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-3.1
Best regards,
Yijing Sun
Monday, February 10, 2020 9:17 AM