Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

add data to more table relation in same time

$
0
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 ef 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
[code]

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 Courses { get; set; }
public List 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; }

}

}
[/code]
my controller empcourse is
[code]
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);
}
}
}

[/code]
my Create view is
[code]
<html><head><metaname="viewport"content="width=device-width"/><title>Create</title><scriptsrc="~/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)
                
 
                Salary:@Html.TextBoxFor(a => a.Salary)
                
 
                Email:@Html.TextBoxFor(a => a.Email)
                
 
                Country:@Html.DropDownList("CountryId")
                
 
                City:<selectid="citylist"name="CityId"></select> 
                District:<selectid="districtlist"name="DistrictId"></select> 
                Courses:@Html.DropDownList("CourseId")
                
 
                
 

                <tableid="tb"></table> 
                
 

                Language:@Html.DropDownList("LanaguageId")
                
 
                
 

                <tableid="tb1"></table> 
                
 

                Level:@Html.DropDownList("LevelId")
                
 
                
 

                <tableid="tb2"></table> 
                <inputtype="submit"/></div>
        }
    </div></body></html>
[/code]
my interface and Relation diagram found in this link
http://www.mediafire.com/view/mn44bl69zkrjukp/Interface3.jpg

Viewing all articles
Browse latest Browse all 3938

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>