none
Cannot add record in ProjectEmployee Table when Employee drop down changed and show in same view RRS feed

  • Question

  • 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

    namespace LinqProject.Controllers
    {   
     public class ProjectEmployeeController : Controller   
     {      
      mytaskdbEntities db = new mytaskdbEntities();     
       // GET: ProjectEmployee       
     public ActionResult Index()       
     {     
           return View(db.ProjectEmployees.ToList());    
        }       
     public ActionResult Create()
            {
    ViewBag.EmployeeId =new SelectList(db.Employees.ToList(),"Id", "fname");
                ViewBag.ProjectId = new SelectList(db.Projects.ToList(),"Id", "Projectname");
                return View();
            }
            [HttpPost]
            public ActionResult Create(ProjectEmployee PE)
            {
                db.ProjectEmployees.Add(PE);
                db.SaveChanges();
                return view();
                
            }
        }
    }

    my ProjectEmployee view  is (show what added in sql server in table tr td)

    @model LinqProject.Models.ProjectEmployee
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Create</title>
        <script src="~/Scripts/jquery-1.10.2.js"></script>
        <script>
            $(function () {
                $("#EmployeeId").change(function () {
                    $.ajax(
                    {
                        type: "POST", //HTTP POST Method  
                        url: "ProjectEmployee/Create", // Controller/View   
                        data: { //Passing data  
                            Project: $("#ProjectId").val(),   
                            Employee: $("#EmployeeId").val(),
    
                        }
    
                    });
    
                });
            });
        </script>
    </head>
    <body>
        <div> 
            @using (Html.BeginForm())
            {
                <div>
                    Employee:@Html.DropDownList("EmployeeId")
                </div>
                <div>
                    Project:@Html.DropDownList("ProjectId")
                </div>
                <input type="submit" value="save" />
            }
        </div>
    </body>
    </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

                                                  
    Thursday, August 18, 2016 8:53 PM

Answers

  • Hi eng-ahmed-salah,

    >>"how to add record when drop down employee changed"

    Did you encounter a exception when executing following code? Was PE null?

    db.ProjectEmployees.Add(PE);
    db.SaveChanges();
    

    The reason is the JSON key name is not equals to the property name of ProjectEmployee class. Please update your JSON key names to meet your requirement.

        <script>
            $(function () {
                $("#EmployeeId").change(function () {
                    $.ajax(
                    {
                        type: "POST", //HTTP POST Method  
                        url: "ProjectEmployee/Create", // Controller/View   
                        data: { //Passing data  
                            ProjectId : $("#ProjectId").val(),   
                            EmployeeId: $("#EmployeeId").val(),
    
                        }
    
                    });
    
                });
            });
        </script>

    >>"show the record added in same view"

    I am not familiar with JavaScript and DOM, for this issue, we suggest you post it on ASP.NET forum for dedicated support.

    http://forums.asp.net/

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 19, 2016 8:34 AM

All replies

  • how to add record when drop down employee changed
    Friday, August 19, 2016 5:51 AM
  • Hi eng-ahmed-salah,

    >>"how to add record when drop down employee changed"

    Did you encounter a exception when executing following code? Was PE null?

    db.ProjectEmployees.Add(PE);
    db.SaveChanges();
    

    The reason is the JSON key name is not equals to the property name of ProjectEmployee class. Please update your JSON key names to meet your requirement.

        <script>
            $(function () {
                $("#EmployeeId").change(function () {
                    $.ajax(
                    {
                        type: "POST", //HTTP POST Method  
                        url: "ProjectEmployee/Create", // Controller/View   
                        data: { //Passing data  
                            ProjectId : $("#ProjectId").val(),   
                            EmployeeId: $("#EmployeeId").val(),
    
                        }
    
                    });
    
                });
            });
        </script>

    >>"show the record added in same view"

    I am not familiar with JavaScript and DOM, for this issue, we suggest you post it on ASP.NET forum for dedicated support.

    http://forums.asp.net/

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 19, 2016 8:34 AM