Hi lingers,
In order to insert the value of ProID into the VendorID, you need to update the inserted product VendorID in the table.
After insert the Product, create instance of Product model class and set the VendorID using the inserted Product.
Refer below sample and modify your code accordingly.
SQL
I have used the Products table with the schema as below.
CREATE TABLE Products
(
ProdID INT PRIMARY KEY IDENTITY,
ProdName VARCHAR(50) NOT NULL,
VendorID INT
)
Model
[Table("Products")]
public class Product
{
[Key]
public int ProdID { get; set; }
[Required(ErrorMessage = "Product Name is required.")]
public string ProdName { get; set; }
public int VendorID { get; set; }
}
DBContext
public class DBCtx : DbContext
{
public DBCtx()
{
}
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<Product> Products { get; set; }
}
IProductRepository class
public interface IProductRepository
{
string InsertProdData(Product product);
}
Repository class
public class Repository: IProductRepository
{
private DBCtx _context { get; }
public Repository(DBCtx context)
{
this._context = context;
}
public string InsertProdData(Product prod)
{
string msg = string.Empty;
try
{
// Insert the Product.
_context.Products.Add(prod);
_context.SaveChanges();
// Update the Product with inserted ProdID.
Product updatedProd = prod;
updatedProd.VendorID = updatedProd.ProdID;
_context.SaveChanges();
msg = "success";
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}
}
Controller
public class HomeController : Controller
{
private IProductRepository objBL;
public HomeController(IProductRepository _productRepository)
{
this.objBL = _productRepository;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(Product prod)
{
if (ModelState.IsValid)
{
string msg = objBL.InsertProdData(prod);
if (msg == "success")
{
return RedirectToAction("Index");
}
else
{
ViewBag.ErrorInfo = "Data not saved, try again latter";
}
}
return View();
}
}
View
@model EF_Core_7_MVC.Models.Product
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
</head>
<body class="container">
<h4>Product</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Index">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="ProdName" class="control-label"></label>
<input asp-for="ProdName" class="form-control" />
<span asp-validation-for="ProdName" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
@if (ViewBag.ErrorInfo != null)
{
<script type="text/javascript">
window.onload = function () {
alert("@ViewBag.ErrorInfo");
};
</script>
}
</body>
</html>
Screenshots
The Form
Inserted Record