将列表框中选定的项目ID保存到数据库中

时间:2015-10-04 17:06:43

标签: c# jquery asp.net-mvc listbox

我想将id列表传递给ASP MVC中的sql数据库。在这里有两个列表框,从一个列表框到另一个列表框能够传递选定的课程名称,然后需要保存那些包含学生ID的课程ID。这需要如何使用jQuery?请帮帮我。这是我现在的代码..

创建视图

创建

.....

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...............");

        }
    });
})

控制器

[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();
    }

我该怎么做?两个列表框是这样的。第二个列表框中的项目需要与关联的studentid一起保存在数据库中。

list box like this

1 个答案:

答案 0 :(得分:0)

您可以将其另存为JSON(objets)字符串

查看How To Populate Html.Listbox with Ajax result using mvc 4