如何选择下拉列表值传递给Ajax.ActionLink在MVC 4?(How to pass sel

2019-09-21 17:16发布

我试图通过一个表格值“客户ID”(iedropdownlist选择的值)在MVC 4.使用Ajax.Actionlink谁能告诉我什么,我做错了什么?

<div class="editor-label">
    @Html.LabelFor(model => model.CustomerID, "Customer Name")
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.CustomerID, Model.CustomersList, "-- Select --")
    @Html.ValidationMessageFor(model => model.CustomerID)
</div>

<div id="ApptsForSelectedDate">
   @Ajax.ActionLink("Click here to view appointments",
                    "AppointmentsList",
                    new {id = Model.CustomerID},
                    new AjaxOptions
                    {
                       UpdateTargetId = "ApptsForSelectedDate",
                       HttpMethod = "GET",
                       InsertionMode = InsertionMode.Replace,
                       LoadingElementId = "progress"
                    }
                  )            
</div>
<div id="progress">
   <img src="../../Images/ajax-loader.gif" alt="loader image" />
</div>

我的控制器方法是这样的:

public PartialViewResult AppointmentsList(int id)
{   ... }

Answer 1:

您应该使用Ajax.BeginForm ,把下拉表单内。 这样,该值将被自动传递。

不幸的是,因为你不能嵌套的HTML表单,如果你已经有了另一种形式的包装这个标记不能使用嵌套的表格。

在这种情况下,你可以使用正常的链接:

@Html.ActionLink(
    "Click here to view appointments", 
    "AppointmentsList", 
    null, 
    new { id = "applink" }
)

并在单独的JavaScript文件AJAXify它并通过读取此刻的AJAX请求发送所选择的下拉值附加必要的信息来查询字符串:

$(function() {
    $('#applink').click(function() {
        $('#progress').show();
        $.ajax({
            url: this.href,
            type: 'GET',
            data: { id: $('#CustomerID').val() },
            complete: function() {
                $('#progress').hide();
            },
            success: function(result) {
                $('#ApptsForSelectedDate').html(result);
            }
        });
        return false;
    });
});


文章来源: How to pass selected dropdownlist value to Ajax.ActionLink in MVC 4?