locked
Create ASP.Net MVC submit form, insert into multiple SQL tables Entity Framework RRS feed

  • Question

  • User-1961814522 posted

    Hi,

    I am new to ASP.Net MVC and Entity Framework.

    I have 3 tables Employee Details, Departments and Employee Designation.

    I am creating a ASP.Net MVC View to add Employees,  which inserts / updates the 3 tables as its joined with Primary Key / Foreign Key relationships.

    The form will be having a Dropdown with Departments  / Designation and text boxes to fetch Employee Name / Age / Address etc. Ideally the Dropdowns (i.e Departments / Designation ) should fetch the list of values from the respective tables.  

    Any sample / tutorials which can help to implement the above scenario.

    Thanks in Advance,

    Ashvin 

    Saturday, May 23, 2020 6:36 PM

All replies

  • User475983607 posted

    The official tutorials illustrate fundamental concepts like updating related data.

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/

    Saturday, May 23, 2020 7:14 PM
  • User1686398519 posted

    Hi,  ashvinvee

    I made an example according to your needs, please refer to it.

    Note: The following are the links for your reference.

    1. Create a more complex data model:https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application
    2. Get Started with Entity Framework 6:https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

    Model

    public class EmployeeDesignation
        {
            [Key]
            public int EmployeeDesignationId { get; set; }
            public string EmployeeDesignationName { get; set; }
        }
        public class Department
        {
            [Key]
            public int DepartmentId { get; set; }
            public string DepartmentName { get; set; }
        }
    
    public class EmployeeDetail
        {
            [Key]
            public int EmployeeId { get; set; }
            public string EmployeeName { get; set; }
            public string EmployeeAge { get; set; }
            public string EmployeeAddress { get; set; }
            public int DepartmentId { get; set; }
            [ForeignKey("DepartmentId")]
            public Department Department { get; set; }
            public int EmployeeDesignationId { get; set; }
            [ForeignKey("EmployeeDesignationId")]
            public EmployeeDesignation EmployeeDesignation { get; set; }
        }

    Controller

    public ActionResult Index()
         {
             var employeeDetails = db.EmployeeDetails.Include(e => e.Department).Include(e => e.EmployeeDesignation);
             return View(employeeDetails.ToList());
         }
    
         public ActionResult Create()
         {
             ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentName");
             ViewBag.EmployeeDesignationId = new SelectList(db.EmployeeDesignation, "EmployeeDesignationId", "EmployeeDesignationName");
             return View();
         }
    
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "EmployeeId,EmployeeName,EmployeeAge,EmployeeAddress,DepartmentId,EmployeeDesignationId")] EmployeeDetail employeeDetail)
         {
             if (ModelState.IsValid)
             {
                 db.EmployeeDetails.Add(employeeDetail);
                 db.SaveChanges();
                 return RedirectToAction("Index");
             }
             ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentName", employeeDetail.DepartmentId);
             ViewBag.EmployeeDesignationId = new SelectList(db.EmployeeDesignation, "EmployeeDesignationId", "EmployeeDesignationName", employeeDetail.EmployeeDesignationId);
             return View(employeeDetail);
         }

    Create

    @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()
        
        <div class="form-horizontal">
            <h4>EmployeeDetail</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.EmployeeName, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.EmployeeName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.EmployeeName, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.EmployeeAge, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.EmployeeAge, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.EmployeeAge, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.EmployeeAddress, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.EmployeeAddress, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.EmployeeAddress, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.DepartmentId, "DepartmentId", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("DepartmentId", null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.DepartmentId, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.EmployeeDesignationId, "EmployeeDesignationId", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("EmployeeDesignationId", null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.EmployeeDesignationId, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
    

    Here is the result.

     
    Best Regards,

    YihuiSun

    Tuesday, May 26, 2020 5:54 AM