更新选择列表中的局部视图 [英] Update a select list in a partial view

查看:97
本文介绍了更新选择列表中的局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的标记在我的网页

I have the following markup on my page

<td colspan="2">
    <table border="0" cellpadding="3" cellspacing="1" width="100%">
    <tr><td colspan="2"><%= Html.LabelFor(model => model.CustomerID)%></td></tr>
    <tr>
        <td width="85%">
            <%= Html.DropDownListFor(model => model.CustomerID, Model.CustomerList, new { id = "customerSelect", style = "width: 380px" })%>
            <%= Html.ValidationMessageFor(model => model.CustomerID, "*")%>
        </td>
        <td width="15%"><button id="btnAddCustomer" style="font-size: 0.7em;">Add new Customer</button></td>
    </tr>
    </table>
</td>

btnAddCustomer 模态对话框中的用户点击与窗体中添加一个新的客户开放。它编制表格,然后preSS保存按钮。

When the user click on the btnAddCustomer a modal dialog is open with the form to add a new customer. It compile the form and then press the save button.

我怎样才能刷新选择元素,包括最新加入的客户,并选择它?

How can I refresh the select element to include the latest added customer and select it?

我应该使用AJAX?

推荐答案

我已经结束了在使用JSON来获取选项,添加到选择使用jQuery操纵

I've ended up on using JSON to get the options and added to select using jquery manipulation

下面code做的招数

function refreshCustomers() {
    $.ajax({
        type: "post",
        dataType: "json",
        url: '<%= Url.Content("~/Contact/GetCustomers") %>',
        async: false,
        data: AddAntiForgeryToken({}),
        success: function (response) {
            $('#customerSelect').html('');
            $.each(response, function (i, customer) {
                $('#customerSelect').append($("<option></option>").attr("value", customer.Value).text(customer.Text));
            });
        }
    });
}

这篇关于更新选择列表中的局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆