如何将选定的DropDownList项ID发送到ActionMethod

时间:2016-02-27 18:37:32

标签: asp.net asp.net-mvc asp.net-mvc-4 razor

我想将DropDownList中所选项目的ID发送到ActionMethod ....当我尝试此代码并选择我想要的项目然后单击提交时,我没有结果..

我的虚拟机要获取项目列表:

 vm.Group = new SelectList(db.Groups.Where(p => p.User_Id == user_id && p.state==1), "Group_id", "Group_name");

我的观点:

@Html.DropDownList("group_list", Model.Group, null, htmlAttributes: new { @class = "form-control" })
<button type="button" data-id="@Model.Book.Book_id" class="BGButton btn btn-primary">Save changes</button>

我的JS电话:

$('.BGButton').click(function () {

    var myId = $(this).data('id');

    $.ajax({
        type: "GET",
        url: '@Url.Action("Insert", "Group")?Book_id=' + myId+"&Group_id="+@Model.Group,
        success: function (response) {
            $('#myElem').html(response).fadeIn('slow');
            $('#myElem').delay(8000).fadeOut('slow');
        },
        failure: function (response) {

            alert("Failure");
        }
    });
});

我的操作我正在尝试联系/向其发送数据:

public ActionResult Insert(int? Book_id , string group_list)
    {

                var x = User.Identity.GetUserId();
                GroupBook GrpBook = new GroupBook()
                    {
                        Book_Id = (int)Book_id,
                        User_Id = x.ToString(),
                        Group_id=Convert.ToInt16(group_list)

                };
                    db.GroupBooks.Add(GrpBook);

    }

当我尝试使用以前的代码时,我没有结果(没有调用),我如何解决这个问题并使其工作,并且谢谢...

1 个答案:

答案 0 :(得分:0)

点击按钮

尝试此操作
$('.BGButton').click(function() {

  var myId = $(this).data('id');
  var groupId = $('#Group_id').val();//assuming Group_id is your dropdown id

      $.ajax({
        type: "GET",
        url: '@Url.Action("Insert", "Group")?Book_id=' + myId + "&Group_id=" + groupId,
        success: function(response) {
          $('#myElem').html(response).fadeIn('slow');
          $('#myElem').delay(8000).fadeOut('slow');
        },
        failure: function(response) {

          alert("Failure");
        }
      });
    });