我是javascript和ajax的新手。我的问题是,我试图将从下拉列表中选择的值传递给方法的ajax调用,以显示部分视图。
这是我的索引视图:
@ModelType FCSWebMVC.Models.SubmitBaselineSelectLists
@Code
ViewData("Title") = "Status Area"
End Code
<h4>@ViewData("Title")</h4>
<br />
<div>
@Using (Html.BeginForm("GetBaseline", "StatusArea"))
@Html.Label("Select Baseline:")
@Html.DropDownList("cmbBaselineType", Model.m_SelectListBaseline)@<br />@<br />
@<input type="button" value="Select!" />
End Using
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#cmbBaselineType").onchange(function () {
var baseline = $('#cmbBaselineType').val();
$("#viewPlaceHolder").load("@Url.Action("StatusList", "StatusArea")", { Bid: baseline },
function (response, status, xhr) {
if (status == "error") {
alert("An error occurred while loading the results.");
}
});
});
});
</script>
<div id="viewPlaceHolder"></div>
我不确定如何获取下拉列表中所选项目的值,并将其传递给我的控制器中的操作方法StatusList。
这是我的StatusList操作方法:
'action to display current features based on baseline
Public Function StatusList(ByVal search As String, ByVal Bid As Integer) As ActionResult
Return PartialView("_StatusList", m_StatusRepository.GetStatusList(Bid, search))
End Function
答案 0 :(得分:0)
你可以试试这个:
<script type="text/javascript">
$(document).ready(function () {
$("#cmbBaselineType").on('change', function () {
var baseline = $(this).val();
$.ajax({
type: 'GET',
path: 'StatusList',
data: { search: '', Bid: baseline },
success: function(result){
$('#viewPlaceHolder').html(result);
},
error: function(){
//do something
}
});
});});</script>
P.s @Html.DropDownList()将分配一个&#34; id&#34;属性(尽管评论说不然)。