How to insert data to multiple table in same time

How to insert data to multiple table in same time

ahmedsaahmedsa Posts: 3Questions: 2Answers: 0

I need to make multiple insert to multiple table have relation with each other

what i need actually when user click submit

Save the following data

Name,Email,Salary,DistrictId in table Employee

EmployeeId,CourseId in table EmployeeCourse

EmployeeId,LanaguageId,LevelId in table EmployeeLangage

what i write in create function in empcourse controller

my custom model as following

public class Customemployee
{
public string Name { get; set; }
public string Salary { get; set; }

    public string Email { get; set; }
    public int DistrictId { get; set; }

    public List<Empcourse> Courses { get; set; }
    public List<Emplangauge> Langs { get; set; }
}
public class Empcourse
{
    public int Id { get; set; }
    public int EmployeeId { get; set; }
    public int CourseId { get; set; }
}
public class Emplangauge
{
    public int Id { get; set; }

    public int LevelId { get; set; }
    public int LanguageId { get; set; }

}

}

my controller empcourse is

public class empcourseController : Controller
{
mycourseEntities db = new mycourseEntities();
// GET: empcourse
public ActionResult Index()
{
return View();
}
public ActionResult Create()
{
ViewBag.CountryId = new SelectList(db.Countries.ToList(), "Id", "CountryName");
ViewBag.LanaguageId = new SelectList(db.Languages.ToList(), "Id", "LnaguageName");
ViewBag.LevelId = new SelectList(db.Levels.ToList(), "Id", "LevelName");
ViewBag.CourseId = new SelectList(db.Courses.ToList(), "Id", "CourseName");
return View();
}
[HttpPost]
public ActionResult Create(Customemployee cemp)
{
return View();
}
public JsonResult getcitybyid(int id)
{
db.Configuration.ProxyCreationEnabled = false;
return Json(db.Cities.Where(a => a.CountryId == id), JsonRequestBehavior.AllowGet);
}
public JsonResult getdistrictbyid(int id)
{
db.Configuration.ProxyCreationEnabled = false;
return Json(db.Destricts.Where(a => a.CityId == id), JsonRequestBehavior.AllowGet);
}
}
}
my Create view is

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
<script src="~/scripts/jquery-1.10.2.js"></script>
<script>
$(function () {
$("#CountryId").change(function () {
$("#citylist").empty();
// alert("error");
var x = $(this).val();
$.ajax({
url: "/empcourse/getcitybyid",
data: { id: x },
success:function(res)
{
$.each(res, function (i, e) {
$("#citylist").append("<option value='" + e.Id + "'>" + e.CityName + "<option>")

                    });
                }
            });


        });
        $("#citylist").change(function () {
            $("#districtlist").empty();
            // alert("error");
            var y = $(this).val();
            $.ajax({
                url: "/empcourse/getdistrictbyid",
                data: { id: y },
                success: function (res) {
                    $.each(res, function (i, e) {
                        $("#districtlist").append("<option value='" + e.Id + "'>" + e.DistrictName + "<option>")

                    });
                }
            });


        });
        $("#CourseId").change(function () {
            var index = 0;
            var id = $(this).val();
            var txt = $("#CourseId option:selected").text();
            $("#tb").append("<tr><td><input type = 'hidden' name='Courses[" + index + "].CourseId' value='" + id + "'/></td><td>" + txt + "</td><td><input type='button' value='remove' class='r'</td></tr>")

            index++;
        });
        $("#tb").on("click", ".r", function () {
            $(this).parent().parent().hide();
            $(this).parent().prev().prev().find("input").val("0");
        });
        $("#LanaguageId").change(function () {
            var index1 = 0;
            var id1 = $(this).val();
            var txt1 = $("#LanaguageId option:selected").text();
            $("#tb1").append("<tr><td><input type = 'hidden' name='Langs[" + index1 + "].LanguageId' value='" + id1 + "'/></td><td>" + txt1 + "</td><td><input type='button' value='remove' class='s'</td></tr>")

            index1++;
        });
        $("#tb1").on("click", ".s", function () {
            $(this).parent().parent().hide();
            $(this).parent().prev().prev().find("input").val("0");
        });

        $("#LevelId").change(function () {
            var index2 = 0;
            var id2 = $(this).val();
            var txt2 = $("#LevelId option:selected").text();
            $("#tb2").append("<tr><td><input type = 'hidden' name='Langs[" + index2 + "].LevelId' value='" + id2 + "'/></td><td>" + txt2 + "</td><td><input type='button' value='remove' class='y'</td></tr>")

            index2++;
        });
        $("#tb2").on("click", ".y", function () {
            $(this).parent().parent().hide();
            $(this).parent().prev().prev().find("input").val("0");
        });
    });
</script>

</head>
<body>
<div>
@using (Html.BeginForm())
{
<div>
Name:@Html.TextBoxFor(a=>a.Name)
<br />
Salary:@Html.TextBoxFor(a => a.Salary)
<br />
Email:@Html.TextBoxFor(a => a.Email)
<br />
Country:@Html.DropDownList("CountryId")
<br />
City:<select id="citylist" name="CityId"></select>
<br />
District:<select id="districtlist" name="DistrictId"></select>
<br />
Courses:@Html.DropDownList("CourseId")
<br />
<br />

            <table id="tb"></table>
            <br />
            <br />

            Language:@Html.DropDownList("LanaguageId")
            <br />
            <br />

            <table id="tb1"></table>
            <br />
            <br />

            Level:@Html.DropDownList("LevelId")
            <br />
            <br />

            <table id="tb2"></table>
            <br />
            <input type="submit" />
        </div>
    }
</div>

</body>
</html>
my interface and Relation diagram is

This discussion has been closed.