Asp.net mvc 使用AJAX返回SelectList选定值

Asp.net mvc 使用AJAX返回SelectList选定值,asp.net-mvc,asp.net-ajax,Asp.net Mvc,Asp.net Ajax,冒着问显而易见的问题的风险。。。我需要使用AJax.ActionLink将SelectList的当前值发送回控制器。我该怎么做 下面是我当前观点的一部分。我需要用SelectList的当前值替换“15” <% If Model.ShoppingListNames IsNot Nothing Then%> <%: Html.DropDownList("ShoppingListNames", Model.ShoppingListNames)%> <%:

冒着问显而易见的问题的风险。。。我需要使用AJax.ActionLink将SelectList的当前值发送回控制器。我该怎么做

下面是我当前观点的一部分。我需要用SelectList的当前值替换“15”

<% If Model.ShoppingListNames IsNot Nothing Then%>
    <%: Html.DropDownList("ShoppingListNames", Model.ShoppingListNames)%>
    <%: Ajax.ActionLink("Add to List", "AdjustMaterials", "Docs",
                        New With {.userDocId = 15, .prodId = Model.ID, .quantity = 1},
                        New AjaxOptions With {.OnSuccess = "handleUpdate"})%>
<% End If%>

使用“答案”来使用Ajax.BeginForm而不是Ajax.ActionLink,下面是我最终得到的视图的一部分

        <% If Model.ShoppingListNames IsNot Nothing Then%>
            <% Using Ajax.BeginForm("AdjustMaterials", "Docs", New With {.prodId = Model.ID}, New AjaxOptions With {.UpdateTargetId = "result-message"})%>
                <%: Html.DropDownList("userDocId", Model.ShoppingListNames)%>
                <input value ="Add to List" type ="submit"/>
            <% End Using%>
            <div id="result-message"></div>
        <% End If%>

下面是控制器。请注意,prodId参数是在对Ajax.BeginForm的调用中指定的,但userDocId参数是由表单中SelectList的当前值指定的

<HttpPost()>
Function AdjustMaterials(ByVal userDocId As Integer, ByVal prodId As Integer,
    Optional ByVal quantity As Integer = 1, Optional ByVal itemTag As String = Nothing) As ActionResult

 ' Do stuff...
End Function

功能调整材料(ByVal userDocId为整数,ByVal prodId为整数,
可选ByVal数量为整数=1,可选ByVal itemTag为字符串=Nothing)作为ActionResult
“做点什么。。。
端函数

最好使用jquery,如下所示:

$(document).ready(function () {
  $('#ShoppingListNames').change(function() {
    var value = $('#ShoppingListNames > option:selected').attr('Value');
    if(value != '') {
      $.ajax({
        type: 'GET',
        contentType: 'application/json;charset=utf-8',
        url: '<%: Url.Action("AdjustMaterials", "Docs", new {prodId = Model.ID, quantity = 1}) %>?=userDocId=' + value,
        data: '',
        dateType: 'json',
        success: function (data) {
          ...
        }
      }
    }
  }
}

JsonRequestBehavior.AllowGet,因为我使用GET,但如果使用POST,则不必这样做。实现这一点的一种方法是将dropdownlist放在AJAX表单中,并用submit按钮替换AJAX链接。单击按钮时,它将使用AJAX将表单提交到所需操作。

看起来很有希望。。。我会在星期一检查,然后告诉你进展如何。谢谢我正在核对你的建议作为答案,因为它符合我的技能,你给我指出了正确的方向。谢谢我发现这个关于AJAX表单的链接很有帮助:
    public ActionResult AdjustMaterials(string prodId, string quantity, string userDocId)
    {
        ...
        return Json("...", JsonRequestBehavior.AllowGet);
    }