For Example I have Two Models AssetMasterModel and AssigningAssetModel. I created a record in assigned asset with asset code (laptop01), employee name (John) and date (08/10/2021).
Then I created another record with the same asset code (laptop01) but different employee name (Dana) and date(09/10/2021). in other words I have assigned this asset to different employee.
How to capture the old Date (08/10/2021) record I have previously created in assigningAssetMode along with the new date I have created (09/10/2021)l and display it in asset master where I Show that this asset was assigned to this employee from this date to this date
What I have tried:
AssigningAssetModel Class:
public class AssigningAssetModel
{
[key]
public int ID { get; set; }
[Required(ErrorMessage = "Enter Employee Name")]
public string EmployeeName {get; set; }
[Required(ErrorMessage = "Enter AssetCode")]
public string AssetCode { get; set; }
[Required(ErrorMessage = "Enter Date")]
public string Date{ get; set; }
}
AssetMasterModel Class:
public class AssetMasterModel
{
[Key]
public int ID { get ;set; }
[Required(ErrorMessage = "Enter AssetCode")]
public string AssetCode { get; set; }
[Required(ErrorMessage = "Enter Employee Name")]
public string EmployeeName{ get; set; }
public string AssignedToDateOld { get; set; }
public string AssignedToDateNew { get; set; }
}
AssigningAsset Controller
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<iactionresult> Create(int id, [Bind("ID,EmployeeName,AssetCode,Date,")] AssigningAssetModel assigningAssetModel)
{
if (id != assigningAssetModel.ID)
return NotFound();
if (!ModelState.IsValid)
return View(assigningAssetModel);
try
{
var masterExist = await _context.Set<assetmastermodel>()
.FirstOrDefaultAsync( i => i.AssetCode == assigningAssetModel.AssetCode);
if (masterExist == null)
return NotFound();
if (masterExist.EmployeeName != assigningAssetModel.EmployeeName)
{
masterExist.EmployeeName = assigningAssetModel.EmployeeName;
masterExist.AssignedToDateNew = assigningAssetModel.Date;
_context.Entry(masterExist).State=EntityState.Modified;
}
_context.Add(assigningAssetModel);
await _context.SaveChangesAsync();
_context.Update(assigningAssetModel);
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
return RedirectToAction(nameof(Index));
}
AssetMaster Controller
public async Task<iactionresult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var assetMasterModel = await _context.AssetMaster
.FirstOrDefaultAsync(m => m.ID == id);
if (assetMasterModel == null)
{
return NotFound();
}
return View(assetMasterModel);
}