0 Replies - 420 Views - Last Post: 18 August 2016 - 04:21 PM

#1 ahmedba  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 24-January 14

Cannot add record in ProjectEmployee Table when Employee drop down cha

Posted 18 August 2016 - 04:21 PM

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

Is This A Good Question/Topic? 0
  • +

Page 1 of 1