如何从DropdownList获取选定的项目?

时间:2019-04-10 06:52:12

标签: model-view-controller html-select

我有一张表格,该表格在其各行中显示课程,应该为每门课程选择一位老师。 我想从下拉列表中获取TeacherId,并将其通过链接按钮标记作为参数发送到操作。单击每一行中的链接按钮后,如何从dropdownList中获取TeacherID?我找不到我该怎么做。

视图:

<table class="table table-bordered">
            <thead>
                <tr>
                    <th hidden="hidden">index</th>
                    <td>course name</td>
                    <td>select teacher</td>
                    <td></td>
                </tr>
            </thead>
<tbody>
                @foreach (var item in Model.Courses)
                {
                    <tr>
                        <td hidden="hidden">@item.CourseId</td>
                        <td>@item.CourseName</td>
                        
                        <td>
                         @{
                                List<SelectListItem> listItems5 = new List<SelectListItem>();

                                listItems5.Add(new SelectListItem
                                {
                                    Text = "please select!",
                                    Value = "0"

                                });
                                foreach (var item1 in Model.teacherCourses)
                                {
                                    if(item1.CourseId==item.CourseId)
                                    {
                                        listItems5.Add(new SelectListItem
                                        {
                                            Text = item1.TeacherName,
                                            Value = item1.TeacherId.ToString(),

                                        });
                                    }


                                }
                            }
   @Html.DropDownListFor("teachers",new SelectList(listItems5, "Value","Text"))
                   </td>

                        <td>
                            <a class="btn btn-danger btn-sm" href="/CourseManagment/AddOrEditTeacherCourses/?TeacherId=**how can I get Teacher ID** & CourseId=@item.CourseId">select teacher</a>
                            
                        </td>
                     </tr>

                                }
            </tbody>

<table>

动作:

public ActionResult AddOrEditTeacherCourses(int TeacherId, int CourseId)
    {
       EI.Teacher = rep.FindTeacherById(TeacherId);
       EI.Courses = rep.selectCourses();
       EI.Course = rep.FindCourseById(CourseId);
        return View("Index", EI);
    }

1 个答案:

答案 0 :(得分:0)

您可以通过下拉的onchange事件来实现,并替换按钮/链接的href

function fnOnchangeyourFunctionname(elm)
 {
            var tr = $(elm).closest('tr');
            var courseId = $(tr).find('td:eq(0)').text().trim();
            var teacherId = $(tr).find('select :selected').val();
            var url = "/CourseManagment/AddOrEditTeacherCourses/?TeacherId="+teacherId+"&CourseId="+courseId;
            $(tr).find('a').attr("href", url)

 }