-1

I want to pass list of ids into sql database in ASP MVC. In here there are two listbox where from one to other listbox able pass selected course names and then need to save those course ids with including the student id. How is this need to do using the jQuery? please help me on this. Here is my code upto now..

Create View

Create

.....

using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>StudentCourseVM</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.StudentId)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.StudentId)
        @Html.ValidationMessageFor(model => model.StudentId)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.StudentName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.StudentName)
        @Html.ValidationMessageFor(model => model.StudentName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.StudentAddress)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.StudentAddress)
        @Html.ValidationMessageFor(model => model.StudentAddress)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.CourseName)
    </div>
<div class="editor-field">     
        <table>    
            <tr>
                <td> Available Courses <br />
                   <select multiple="multiple" id="listboxCourse">
                           @foreach (var item in Model.courseList) 
                               { 
                                   <option title="@item.CourseName" id="@item.CourseID">
                                       @item.CourseName
                                   </option>
                               }
                   </select>
                </td>
                <td>
                            <button type="button" id="btnAdd">Move Right>></button>
                            <br/>
                            <button type="button"  id ="btnRemove">Move Left<<</button>
                </td>
                <td> Selected Courses<br />
                    <select multiple="multiple" id="addcourselist" >                                             
                    </select>
                </td>
            </tr>                   
        </table>        
    </div>
<p>
        <input type="submit" value="Create" id="Create" />
    </p>
</fieldset>
}

<script>
 $(document).ready(function() {
        $("#btnAdd").click(function() {
            $("#listboxCourse > option:selected").appendTo("#addcourselist");
        });


        $("#btnRemove").click(function() {
            $("#addcourselist > option:selected").appendTo("#listboxCourse");
        });

    })            
$(function () {
    $("#Create").click(function () {
        var listCourse = new Array();
        for (var i = 0; i < addcourselist.length ; i++) {
            listCourse.push(Option[i].value);
        }

    });
$.ajax({
        type: "POST",
        data: listCourse,
        dataType : "json",
        url: "Student/Create",
        success: function (data) {
            alert("Code is running...............");

        }
    });
})

Controller

[HttpGet]
    [ActionName("Create")]
    public ActionResult Create()
    {
        StudentCourseVM studentcourse = new StudentCourseVM();
        StudentBusinessLayer studentBusinessLayer = new StudentBusinessLayer();
        List<Course> coursestudentList = studentBusinessLayer.CourseList.ToList();
        studentcourse.courseList = coursestudentList;
        return View(studentcourse);
    }



 [HttpPost]
    [ActionName("Create")]
    public ActionResult Create(StudentCourseVM studentcourse)
    {
        if (ModelState.IsValid)
        {
            StudentBusinessLayer studentBusinessLayer = new StudentBusinessLayer();
            List<Course> coursestudentList = studentBusinessLayer.CourseList.ToList();
            studentcourse.courseList = coursestudentList;
            studentBusinessLayer.AddStudent(studentcourse);
            return RedirectToAction("Index");
        }
        return View();
    }

How am i supposed to do this. Two listbox are like this. The items in second listbox need to be saved in database with the relate studentid.

list box like this

tereško
  • 58,060
  • 25
  • 98
  • 150
user4377924
  • 25
  • 1
  • 5
  • Why do you need 2 listboxes? If you have just one and correctly bind it to your model the the items you select in the first one will be bound to the model. Currently you not binding to anything and nothing will be posted back because your listboxes don't even have name attributes –  Oct 05 '15 at 00:20
  • i need to use two listboxes anyway. How it need to bind to model? – user4377924 Oct 05 '15 at 03:35
  • Why do you need 2 listboxes. Do you understand that only the values that are actually selected will be posted - just moving them from one to the other wont help you at all (and will confuse the user who may think they are submitting the options in the RHS listbox when they may not be). In anycase, a checked listbox is a better way to handle this. –  Oct 05 '15 at 03:37
  • So if use checked listbox for this how to POST the checked coursename's ids into controller with including the studentid using the jQuery. – user4377924 Oct 05 '15 at 04:35
  • You use a view model (say) `CourseVM` with properties `int ID`, `string Name` and `bool IsSelected` and use a `for` loop to generate each `CourseVM` in the collection. Then you can get all the selected items where `IsSelected == true` Refer [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example –  Oct 05 '15 at 04:40
  • my requirement is to make a listbox like above. so i think of making like that. – user4377924 Oct 05 '15 at 05:58

1 Answers1

0

You can save it as JSON (objets) strings

Check out How To Populate Html.Listbox with Ajax result using mvc 4

Community
  • 1
  • 1
Zulander
  • 648
  • 1
  • 10
  • 22