Search code examples
jqueryasp.net-mvc-4modalpopup

loading data in dropdownlists in popup in mvc


I am new to asp.net mvc.
i am able to show pop-ups in mvc.
http://www.dotnet-tricks.com/Tutorial/mvc/42R0171112-CRUD-Operations-using-jQuery-dialog-and-Entity-Framework---MVC-Razor.html

I am able to load data in dropdownlist in mvc.
but, i want to load data in dropdownlist which exist inside a pop-up so how to do that.

or i have to load data in dropdownlist before showing it but that will not be best way as user may or may not show popup.

please suggest.


Solution

  • function openPopUp()
    {
      // ur code to open the popup
       LoadDropDownContent(); 
    }
    function LoadDropDownContent()
    {
         var url = $(this).data('url')// url of ur ActionResult;
            var data = //Any data u want to pass on 
            //get the timestamp
            var nocache = new Date().getTime();
            //add the timestamp as a paramter to avoid caching
            data['nocache'] = nocache;
            $.getJSON(url, data, function (items) {
                var ddl = $('#urdrpdownid');
                ddl.empty();
                ddl.append($('<option/>', { value: '', text: '--Selecteer--' }));
                $.each(items, function (index, item) {
                    ddl.append($('<option/>', {
                        value: item.Value,
                        text: item.Text,
                        selected: item.Selected
                    }));
                });
            }); 
    }
    

    Code in your Controller

    public ActionResult GetItems()
    {
         var dropdownitems = //ur BL/DAL function to retrieve the list of entity;
         var items = dropdownitems.Select(s => new SelectListItem { Text = s.ColName, Value = s.ColName}).AsEnumerable();
            return Json(items, JsonRequestBehavior.AllowGet);
    }