如何使用jquery重新加载模型的一部分

时间:2013-08-30 11:40:53

标签: jquery asp.net-mvc

如何使用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。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

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

  1. 在下拉列表上进行ajax调用,以获取数据。
  2. 然后,在ajax调用成功后,您可以使用新数据替换您的视图。
  3. 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 () {
        }
    });
    }