[CLOSED] reload combobox's store from controller action on click of combobox

  1. #1

    [CLOSED] reload combobox's store from controller action on click of combobox

    i have a combobox in razor view with a store defined.
    How can i reload the store (trigger controller action) on every click of the combo

    ////this is the view code
    .Items(Html.X().ComboBox()
                .ID("cmbid")
                .Name("cmbname")
                .Editable(false)
                .FieldLabel("Select value")
                //.TypeAhead(true)
                .Width(250)
                .QueryMode(DataLoadMode.Default)
                .ForceSelection(true)
                .TriggerAction(TriggerAction.All)
                .DisplayField("name")
                .ValueField("id")
                .EmptyText("Loading...")
                .ValueNotFoundText("Loading...")
                .Store(Html.X().Store()
                        .AutoLoad(false)
                        .Model(Html.X().Model()
                            .IDProperty("Id")
                            .Fields(
                                new ModelField("id", ModelFieldType.String) { Mapping = "Id" },
                                new ModelField("name", ModelFieldType.String) { Mapping = "Name" }
                            )
                        )
                        .Proxy(Html.X().AjaxProxy()
                            .Url(Url.Action("controllerstore_Action"))
                            .Reader(Html.X().JsonReader().RootProperty("data"))
                        )
                        .Parameters(ps =>
                            ps.Add(new StoreParameter("parameter_id", "App.textfield1.getValue()", ParameterMode.Raw))
                        )
                        .Listeners(ls =>
                            ls.Load.Handler = @"var combo = App.cmbid;
                                                combo.setValue(records[0].get(combo.valueField));"
                        )
                    )//end store
    
                )//combo items ends
    ///==========controller code
    
     public ActionResult Reload_Combo(string parameter_id)
            {
                try
                {
                    return this.Store(somedata.GetComboData(parameter_id));
                }
                catch (System.Exception)
                {
    
                    throw;
                }
            }
    Last edited by fabricio.murta; Jun 06, 2016 at 9:16 PM.
  2. #2
    Hello @amenyo,

    Please provide a fully working code sample demonstrating how to reproduce the issue. Bits and pieces do not help us reproduce the issue in our end and identify it.

    Also, please wrap your code around [CODE][/CODE] tags as it allows better formatting and reading of the forum thread!

    The code sample you provide should include only the minimum amount of code required to reproduce the issue. Code unrelated to the issue is to be removed. Anyone should be able to copy + paste your sample into a local Visual Studio test project and run without having to make modifications.

    Tips for creating simplified code samples

    If Exceptions or syntax errors are thrown when testing your code sample, we'll let you know so you can revise your original sample. Then we'll review again with the updated sample.

    When posting your code samples in the forums, please paste that sample within [CODE] tags. The [CODE] tags will add formatting and syntax highlighting to your sample.

    The following two forum posts provide many excellent tips for posting in the forums:

    1. More Information Required
    2. Forum Guidelines
    Fabrício Murta
    Developer & Support Expert
  3. #3

    how do i get the controller action to execute on every click of the combobox ?

    //the controller code
     public class ComboLoadController : Controller
        {
            // GET: ComboLoad
    
            ComboData cmbdata = new ComboData();
            public ActionResult Index()
            {
                return View();
            }
    
            public ActionResult GetCities(string country)
            {
                return this.Store(cmbdata.GetCities());
            }
    
    
        }
    //view code

    @(Html.X().ResourceManager())
    
    @(Html.X().Panel()
               .ID("cmd1")
               .Items(Html.X().ComboBox()
                .ID("cmbid")
                .Name("cmbname")
                .Editable(false)
                .FieldLabel("Select value")
                //.TypeAhead(true)
                .Width(250)
                .QueryMode(DataLoadMode.Default)
                .ForceSelection(true)
                .TriggerAction(TriggerAction.All)
                .DisplayField("name")
                .ValueField("id")
                .EmptyText("Loading...")
                .ValueNotFoundText("Loading...")
                .Store(Html.X().Store()
                        .AutoLoad(false)
                        .Model(Html.X().Model()
                            .IDProperty("Id")
                            .Fields(
                                new ModelField("id", ModelFieldType.String) { Mapping = "Id" },
                                new ModelField("name", ModelFieldType.String) { Mapping = "Name" }
                            )
                        )
                        .Proxy(Html.X().AjaxProxy()
                            .Url(Url.Action("GetCities"))
                            .Reader(Html.X().JsonReader().RootProperty("data"))
                        )
                        //.Parameters(ps =>
                        //    ps.Add(new StoreParameter("parameter_id", "App.textfield1.getValue()", ParameterMode.Raw))
                        //)
                        .Listeners(ls =>
                            ls.Load.Handler = @"var combo = App.cmbid;
                                                combo.setValue(records[0].get(combo.valueField));"
                        )
                    )//end store
                )//combo items ends
        )
    // model class

       public class ComboData
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public IEnumerable GetCities()
            {
                List<object> data = new List<object>();
                data.Add(new { Id = "1", Name = "GHANA" });
                data.Add(new { Id = "2", Name = "USA" });
                data.Add(new { Id = "3", Name = "UK" });
                data.Add(new { Id = "4", Name = "NIGERIA" });
                data.Add(new { Id = "5", Name = "SOUTH AFRICA" });
                data.Add(new { Id = "6", Name = "CHINA" });
                return data;
            }
        }
    Last edited by fabricio.murta; Jun 19, 2016 at 5:59 PM. Reason: Wrap [code] tags around code.
  4. #4
    Hello @amenyo! Please notice tagging code under code blocks uses the [code] notation not <code>.

    Here's a review on your view to make the combo box reload every time the user clickers the combo box's trigger:

    
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>c61187_Index</title>
    </head>
    <body>
        <div>
        @(Html.X().ResourceManager())
    
        @(Html.X().Panel()
            .ID("cmd1")
            .Items(Html.X().ComboBox()
                .ID("cmbid")
                .Name("cmbname")
                .Editable(false)
                .FieldLabel("Select value")
                //.TypeAhead(true)
                .Width(250)
                .QueryMode(DataLoadMode.Default)
                .ForceSelection(true)
                .TriggerAction(TriggerAction.All)
                .DisplayField("name")
                .ValueField("id")
                .EmptyText("Loading...")
                .ValueNotFoundText("Loading...")
                .Store(Html.X().Store()
                        .AutoLoad(false)
                        .Model(Html.X().Model()
                            .IDProperty("Id")
                            .Fields(
                                new ModelField("id", ModelFieldType.String) { Mapping = "Id" },
                                new ModelField("name", ModelFieldType.String) { Mapping = "Name" }
                            )
                        )
                        .Proxy(Html.X().AjaxProxy()
                            .Url(Url.Action("c61187_GetCities"))
                            .Reader(Html.X().JsonReader().RootProperty("data"))
                        )
            //.Parameters(ps =>
            //    ps.Add(new StoreParameter("parameter_id", "App.textfield1.getValue()", ParameterMode.Raw))
            //)
                        .Listeners(ls =>
                        {
                            ls.Load.Handler = @"var combo = App.cmbid;
                                                if (combo.getSelection() == null) {
                                                    combo.setValue(records[0].get(combo.valueField));
                                                };";
                            //ls.Refresh.Handler = "this.load();";
                        }
                            
                            
                        )
                    )//end store
                .Listeners(ls => ls.Expand.Handler = "this.store.load();")
                )//combo items ends
        )
        </div>
    </body>
    </html>
    The model & controller will be still the same of the ones you posted.
    Notice here I've just changed your store's load event to avoid reselecting if there was a previous selection (that would dismiss the expanded dropdown) and added the combobox's an expand event handler to force the store reload.

    You probably don't really want the 'setvalue' in the load event, as it makes the first expand of the combo box to be collapsed once the store loads.

    Hope this helps!
    Fabrício Murta
    Developer & Support Expert

Similar Threads

  1. Replies: 0
    Last Post: Oct 24, 2013, 2:53 PM
  2. [CLOSED] ComboBox Not Populating on First Store Reload
    By elisa in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Sep 02, 2013, 4:17 PM
  3. [CLOSED] Combobox Click Trigger allways reload data
    By UnifyEducation in forum 2.x Legacy Premium Help
    Replies: 4
    Last Post: Aug 26, 2013, 5:01 PM
  4. Replies: 2
    Last Post: May 24, 2013, 1:54 AM
  5. [CLOSED] Combobox expands after the store reload
    By deejayns in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Jan 24, 2012, 7:26 AM

Posting Permissions