0

我怎样才能用jquery“重新加载”视图模型的一部分,而不用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。

我怎样才能做到这一点?

4

1 回答 1

1

您可以按照以下步骤操作:

  1. 对下拉列表中的更改进行 ajax 调用,这将获取数据。
  2. 然后在 ajax 调用成功后,您可以用新数据替换您的视图。

阿贾克斯调用:

$("#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 () {
    }
});
}
于 2013-08-30T12:13:02.740 回答