Click here to Skip to main content
15,887,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have 3 classes as Student ,Course and StudentCourses .
Class Student has below fields:
Id,Name,Family

Class course has below fields:
Id,CourseName

Class StudentCourses has below fields:
Id,StudentId,CourseId

In my page I want to choose a StudentNames from dropdown and course names from other Dropdown .user could add several course and each time the new record appear in my table in same page without refresh ,then when user click on submit all the records in table must store in class StudentCourses .I want do this with Json but I dont know how to write it's codes .Anybody has sample ?

Thanks
Posted

1 solution

For that see the following code
Controllers ->
      public JsonResult GetStudents(int id)
       {
           var states = db.Students.Where(x => x.courseID == id).ToList();
           return Json(states, JsonRequestBehavior.AllowGet);
       }

Views -> Jquery
XML
$("#ddlCourse").change(function () {
      var url = "/Controls_/GetStudents";
      var courseID = $("#ddlCourse").val();
      $.post(url, { id: courseID }, function (data) {
          $("#ddlStudent").empty();
          var items = "<option>Select Student<option>";
          $.each(data, function (i, st) {
              items += "<option value=" + st.ID + ">" + st.StudentName + "</option>";
          });
          $("#ddlStudent").html(items);
      });
  });
 
Share this answer
 
v2
Comments
sorosh04 13-Jul-14 20:13pm    
Thanks for your solution,but if I want to choose item several times and pass it to controller one time ,how can I write code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900