刷新/重新绑定一个下拉列表asp.net MVC [英] Refresh/ Rebind one dropdownlist asp.net MVC

查看:64
本文介绍了刷新/重新绑定一个下拉列表asp.net MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表,其中列出了类别&一个用于刷新下拉列表项的链接.

I have a dropdownlist which has list of categories & a link which is for refreshing dropdownlist items.

@Html.DropDownList("CategoryNameItemNameBinding", Model.CatgegoryNameItems)

<a href="javascript:void(0)" id="opener" onclick="openDialog('Category',this)">Refresh Category List</a> </span>

该链接会打开一个带有按钮的弹出窗口,单击该按钮后,下拉列表所绑定的数据库表就会更新.

The link opens a small popup windows with a button which when clicked the database table to which dropdownlist is bound is updated.

问题在于,尽管数据库表已更新,但下拉列表不会显示新项,直到手动刷新页面或我导航离开并返回页面为止.

The problem is that although the database table is updated the dropdownlist does not shows new items until the page is manually refreshed or I navigate away and come back to page.

我不想刷新/重新加载整个页面.如何刷新下拉列表以反映新项目.

I do not want to refresh/ reload the entire page. How do I refresh the dropdownlist to reflect the new items.

谢谢

推荐答案

您的Ajax调用将会

$("#linkId").on('click', function (event) {
            var url = "GetList";
            $.ajax({
                data: {},
                type: 'POST',
                cache: false,
                dataType: 'json',
                url: url,
                success: function (result) {
                    $("#dropDownId").empty();
                    $("#dropDownId").append('<option value="">Select One</option>');
                    $.each(result, function (i, item) {
                        $("#AddItemItemId").append('<option value="' + item.Value + '">' +
                            item.Text + '</option>');
                        // here we are adding option for States
                    });
                },
                error: function (ex) {
                    alertify.alert('We face some technical difficulties. Hello World');
                }
            });
            event.preventDefault(event);
        });

和后端C#代码会喜欢

[HttpPost]
    public JsonResult GetList()
    {

        var itemlist = _itemManager.GetItems();
        var itemList = itemlist.Select(item => new SelectListItem { Text = item.ItemName+" - "+item.PowerName, Value = Convert.ToString(item.Id) }).ToList();
        return Json(new SelectList(itemList, "Value", "Text"));
    }

希望这会对您有所帮助.编码愉快

Hope this will help you. Happy coding

这篇关于刷新/重新绑定一个下拉列表asp.net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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