我正在尝试使用MVC 4中的Ajax.Actionlink传递Form值“CustomerID”(即dropdownlist选择的值)。有人能告诉我这里做错了吗?
<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)
{ ... }
答案 0 :(得分:3)
您应该使用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;
});
});