ahmed salah

ahmed salah

  • 1.6k
  • 530
  • 125.4k

Cannot add record in ProjectEmployee Table in employee drop

Aug 18 2016 7:32 PM
Cannot add record in ProjectEmployee Table when Employee drop down changed and show in same view
I have 3 table join with relation and it have models
Project Table
Id primary key increment identity
Projectname
Employee table
Id primary key increment identity
fname
ProjectEmployee Table
Id primary key increment identity
ProjectId forign key project table
EmployeeId forign key Employee table
in view i have two drop down list
first for Project
second for Employee
I need when i select value from drop down project
then select value from employee added in database then
show on table one view in same page view
sample what i need is
Project drop down user select (project1)
Employee drop down user select name (ahmed)
on change event of employee drop down add record in database and show what added in tr td
project name employee
project1 ahmed
I can fill drop down project and drop down Employee
with data
but
i cannot add data in database sql server when select from employee add record in database and show
what added in table as tr and td
my ProjectEmployee controller as following
  1. namespace LinqProject.Controllers  
  2. {  
  3. public class ProjectEmployeeController : Controller  
  4. {  
  5. mytaskdbEntities db = new mytaskdbEntities();  
  6. // GET: ProjectEmployee  
  7. public ActionResult Index()  
  8. {  
  9. return View(db.ProjectEmployees.ToList());  
  10. }  
  11. public ActionResult Create()  
  12. {  
  13. ViewBag.EmployeeId = new SelectList(db.Employees.ToList(),"Id""fname");  
  14. ViewBag.ProjectId = new SelectList(db.Projects.ToList(),"Id""Projectname");  
  15. return View();  
  16. }  
  17. [HttpPost]  
  18. public ActionResult Create(ProjectEmployee PE)  
  19. {  
  20. db.ProjectEmployees.Add(PE);  
  21. db.SaveChanges();  
  22. return view();  
  23. }  
  24. }  
  25. }  
my ProjectEmployee view is (show what added in sql server in table tr td)
  1. @model LinqProject.Models.ProjectEmployee  
  2. @{  
  3. Layout = null;  
  4. }  
  5.   
  6. <!DOCTYPE html>  
  7.   
  8. <html>  
  9. <head>  
  10. <meta name="viewport" content="width=device-width" />  
  11. <title>Create</title>  
  12. <script src="~/Scripts/jquery-1.10.2.js"></script>  
  13. <script>  
  14. $(function () {  
  15. $("#EmployeeId").change(function () {  
  16. $.ajax(  
  17. {  
  18. type: "POST"//HTTP POST Method  
  19. url: "ProjectEmployee/Create"// Controller/View  
  20. data: { //Passing data  
  21. Project: $("#ProjectId").val(),  
  22. Employee: $("#EmployeeId").val(),  
  23. }  
  24.   
  25. });  
  26.   
  27. });  
  28. });  
  29. </script>  
  30. </head>  
  31. <body>  
  32. <div>  
  33. @using (Html.BeginForm())  
  34. {  
  35. <div>  
  36. Employee:@Html.DropDownList("EmployeeId")  
  37. </div>  
  38. <div>  
  39. Project:@Html.DropDownList("ProjectId")  
  40. </div>  
  41. <input type="submit" value="save" />  
  42. }  
  43. </div>  
  44. </body>  
  45. </html>  
I writing code above and i try to add record in ProjectEmployee table and show what i added in tr td
but how to add record in table projectemployee table
i cannot do it by code
can you modify the code to add record in database and show the record added in same view

Answers (1)