如何使用jquery“重新加载”viewmodel的一部分,而不使用POST
其余形式?
首先,代码:
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@ViewBag.Status
<div id="haveToReload">
@foreach (var type in OrderTools.getDeliveryTypes(int valueFromDropDownList))
{
<div style="width:400px; ">
@Html.RadioButtonFor(x => x.SelectedDeliveryTypeID, type.DeliveryTypeID, new { name = "DeliveryType", id = type.DeliveryTypeID })
<label for="@type.DeliveryTypeID">@type.Name | @type.Price | @type.RealisationDays </label>
</div>
}
</div>
@Html.LabelFor(m => m.CountryID)
@Html.DropDownListFor(m=> m.CountryID, UserTools.getCountries(), new { @class="dropdownlist" })
@Html.ValidationMessageFor(m => m.CountryID)
(...)
}
我的想法是 - 如果用户更改DropDownListFor
的值,它会自动“重新加载”<div id="haveToReload">..</div>
OrderTools.getDeliveryTypes(int valueFromDropDownList)
的新值,并显示特定国家/地区的deliveryTypes。
我该怎么做?
答案 0 :(得分:1)
您可以按照以下步骤操作:
Ajax Call:
$("#dropDownId").change() {
$.ajax({
type: "POST",
url: "Controller/Action",
data: { 'dropDownValue' : "+ $(this).val() +"} ,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
$("#haveToReload").replaceWith(result); // your replacing logic
},
error: function () {
}
});
}