Click here to Skip to main content
15,042,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
so 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

What I have tried:

p
C#
ublic 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; }
    }


public class AssetMasterModel
{
        [Key]
        public int ID { get ;set; }

        public string AssetCode { get; set; }

        [Required]
        public string EmployeeName{ get; set; }
}


AssigningAssetController

[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 Asset Master Table
note that if the asset code is not found it should not create new record in asset master table
Posted
Updated 5-May-21 20:10pm
v2
Comments
Gerry Schmitz 6-May-21 14:53pm
   
It also need to search the "employee" master for "Hana".
Mouffaq Dalloul 8-May-21 1:40am
   
I don't believe it is necessary to search for employee name in employee master because in asset master when creating a new record he employee dropdown field is binded with the employee master
Gerry Schmitz 8-May-21 14:01pm
   
That's the same as what I said. Maybe you need an "asset master dropdown".

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