0
I need to make multiple insert to multiple table have relation with each other
all id in all table is identity and already do model relation to it
in visual studio 2015
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
publicclassCustomemployee{publicstringName{get;set;}publicstringSalary{get;set;}publicstringEmail{get;set;}publicintDistrictId{get;set;}publicList<Empcourse>Courses{get;set;}publicList<Emplangauge>Langs{get;set;}}publicclassEmpcourse{publicintId{get;set;}publicintEmployeeId{get;set;}publicintCourseId{get;set;}}publicclassEmplangauge{publicintId{get;set;}publicintLevelId{get;set;}publicintLanguageId{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>
Abeer Shlby
21-Aug-2016