Click here to Skip to main content
15,038,451 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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:
C#
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
C#
[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
C#
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);
}
Posted
Updated 23-May-21 22:38pm
v2
Comments
Gerry Schmitz 23-May-21 15:07pm
   
The "assignment" identifies who has the asset at any given time and when. Putting "employee info" in the "asset master" itself is "bad design".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900