'Creating a record in one table will update an existing record in another table ASP.NET Core MVC
I am new to ASP.NET Core MVC and I wanted to know how to create a new record in one table and update an existing record in another table for example I have two tables Asset Master and Assigning Asset so when creating a record in Assigning Asset with employee name (eg. Hana) and asset code (eg. Lap001) it should search in Asset Master table if the asset code exist update employee name if not throw error I have searched for the solution but couldn't find the answer
AssigningAssetModel class:
public class AssigningAssetModel
{
[Key]
public int ID { get; set; }
[Required(ErrorMessage = "Enter Employee Name")]
public string Employee Name{ get; set; }
[Required(ErrorMessage = "Enter AssetCode")]
public string AssetCode { get; set; }
}
AssetMasterModel class:
public class AssetMasterModel
{
[Key]
public int ID { get ;set; }
public string AssetCode { get; set; }
[Required]
public string EmployeeName{ get; set; }
}
AssigningAsset controller
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<iactionresult> Create(int id [Bind("ID,EmployeeName,AssetCode")] AssigningAssetModel assigningAssetModel)
{
if (id != assigningAssetModel.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(assigningAssetModel);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!AssigningAssetModelExists(assigningAssetModel.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(assigningAssetModel);
}
How to pass AssetCode here so I can update the existing record in AssetMaster table?
Note that if the asset code is not found it should not create new record in AssetMaster table
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
