5

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.

Thanks

2
  • You can either use ajax to call a controller method that queries the database returns the items to populate in the dropdownlist, or better (since you already know the new values(s) to be added in the view from you popup), just use javascript to append a new option based on the data in the popup Commented Apr 17, 2016 at 13:00
  • Hi Stephen, thanks for replying. I am new to Mvc so can you please post a sample of ajax call. I found many examples but not sure which one is right way to go. Commented Apr 17, 2016 at 15:47

1 Answer 1

2

Your Ajax call will be

$("#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);
        });

and backend C# code will like

[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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this did the job, really appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.