简体   繁体   中英

How to Change Date from new to old date when creating new record in ASP.NET CORE MVC

For example I created a record in assigned asset with asset code (lap001), employee name (hana) and date (08/10/21).

Then I created another record with the same asset code (lap001) different employee name (lana) and date(09/10/21). in other words I have assigned this asset to diffrent employee.

In asset master details of that specific asset code (lap001) screen how to show that this specific asset was assigned to this employee from previous date (08/10/21) to new date (09/10/21).

Now currently Date in AssigningAssetModel is assigned to AssignedToDateNew now how to move the new date to old date when I create a new record and also how to prevent user to put date between previous date (08/10/21) to new date (09/10/21)

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);
    }

So basically in Assigning Asset Index this asset code (laptop0001015) is first assigned to Tom at 22/05/2021 then it has been assigned to Mouffaq at 24/05/2021 now in assetmaster how to show that this asset code in this case (Laptop0001015) was assigned to Tom From 22/05/2021 to 24/05/2021

From what i understand, what you need to do is keep the existing asset date before creating the new record and then assign it to the new record.

prevDate = employee.getCurrentAsset().date
newAsset = new Asset()
newAsset.date = prevDate ;
employee.setCurrentAsset(newAsset) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM