[CLOSED] MVC How to load combobox via ViewBag?

  1. #1

    [CLOSED] MVC How to load combobox via ViewBag?

    Hi,
    Like in question how to load combobox with items (Ext.Net.ListItem)

    I tryed to do this:
    controller
    public ActionResult Create()
            {
                ViewBag.Type = new List<Ext.Net.ListItem> { new Ext.Net.ListItem("Odbiorca", 1), new Ext.Net.ListItem("Dostawca", 2), new Ext.Net.ListItem("Odbiorca / Dostawca", 4) };
                return View();
            }
    create.cshtml
    @{
    List<Ext.Net.ListItem> listType = (List<Ext.Net.ListItem>)ViewBag.Type;
    }
    ....
    Html.X().ComboBoxFor(m=>m.Type).Editable(false)
                    .ForceSelection(true).EmptyText("-- choose--").EmptyValue(0)
                    .Items(listType)
    
    ....
    Edited:
    This approach work just fine but please see my second post for better understanding this question.

    How should I load this combobox with items or how is best approach to load combobox items(created in controller method) Is there a object which work similar to SelectList?


    Thanks,
    ViDom
    Last edited by Daniil; Jun 11, 2013 at 4:26 AM. Reason: [CLOSED]
  2. #2
    Hello!

    Try the following:

    Contoller:
    public ActionResult Index()
    {
    	ViewBag.Data = new List<Ext.Net.ListItem> { new Ext.Net.ListItem("Odbiorca", 1), new Ext.Net.ListItem("Dostawca", 2), new Ext.Net.ListItem("Odbiorca / Dostawca", 4) };
    	return View();
    }
    View:
    @model List<Ext.Net.ListItem> 
    
    @(Html.X().ResourceManager())
    
    @(Html.X().ComboBox().Editable(false)
        .ForceSelection(true).EmptyText("-- choose--").EmptyValue(0)
        .Items(ViewBag.Data)
    )
  3. #3
    Quote Originally Posted by Baidaly View Post
    Hello!

    Try the following:

    Contoller:
    public ActionResult Index()
    {
    	ViewBag.Data = new List<Ext.Net.ListItem> { new Ext.Net.ListItem("Odbiorca", 1), new Ext.Net.ListItem("Dostawca", 2), new Ext.Net.ListItem("Odbiorca / Dostawca", 4) };
    	return View();
    }
    View:
    @model List<Ext.Net.ListItem> 
    
    @(Html.X().ResourceManager())
    
    @(Html.X().ComboBox().Editable(false)
        .ForceSelection(true).EmptyText("-- choose--").EmptyValue(0)
        .Items(ViewBag.Data)
    )
    Hi @Baidaly,
    Firstly I want to appologize for bother and not edited my post previously but my solution is work just fine.
    Secondly my Title can be little confusing. I meant here I wanted to know what is best approach to load combobox without making explicit definition of combobox on View( my solution work but just for creating new item what if there will be some selected value in there?). Hope this answer will clear my question purpose.
  4. #4
    Quote Originally Posted by ViDom View Post
    I meant here I wanted to know what is best approach to load combobox without making explicit definition of combobox on View
    Not sure, but, maybe, you need a FormPanelFor feature.
    http://mvc.ext.net/#/Models/FormPanelFor/

    Quote Originally Posted by ViDom View Post
    my solution work but just for creating new item what if there will be some selected value in there?
    Well, you can populate a ComboBox's SelectedItems in a similar way. Is that not an option for you?
  5. #5
    Quote Originally Posted by Daniil View Post
    Not sure, but, maybe, you need a FormPanelFor feature.
    http://mvc.ext.net/#/Models/FormPanelFor/



    Well, you can populate a ComboBox's SelectedItems in a similar way. Is that not an option for you?
    It's double work than in ASP.NET MVC because I need to pass List of all items and another element which should be selected.
  6. #6
    I've face next problem when I load List<ListItem> to combobox items collection (in postback I got always null even when I choose any item) so this solution isn't good.

    Please advice how should I handle this.

    Here is example which should fit my needs:http://mvc.ext.net/#/Form_ComboBox/Ajax_Linked_Combos/

    but I don't understand "this.Store()" - it's mean that is store which caused callback to this method "GetCities()" in example?
    Last edited by ViDom; Jun 05, 2013 at 7:11 PM.
  7. #7
    Quote Originally Posted by ViDom View Post
    It's double work than in ASP.NET MVC because I need to pass List of all items and another element which should be selected.
    I don't any other options, because any way you should pass list of values for the combobox and then set selected value. I don't understand why it's a double work?
  8. #8
    Quote Originally Posted by ViDom View Post
    I've face next problem when I load List<ListItem> to combobox items collection (in postback I got always null even when I choose any item) so this solution isn't good.

    Please advice how should I handle this.
    There is no PostBack in MVC. You just submit values to Controller's Action which handles submitted values and returns response. So if I understood right you should check in FireBug or other developers tool what values you submit.

    Also, we will be happy to investigate your sample.

    Quote Originally Posted by ViDom View Post
    Here is example which should fit my needs:http://mvc.ext.net/#/Form_ComboBox/Ajax_Linked_Combos/

    but I don't understand "this.Store()" - it's mean that is store which caused callback to this method "GetCities()" in example?
    No, it's not related to the store which caused callback. this.Store() returns StoreResult and contains values for the Store. You should use it if your Action should return values for a store.

Similar Threads

  1. [CLOSED] ComboBox load value
    By FpNetWorth in forum 2.x Legacy Premium Help
    Replies: 8
    Last Post: Dec 27, 2012, 9:54 AM
  2. [CLOSED] ViewBag and Razor
    By zwf in forum 2.x Legacy Premium Help
    Replies: 4
    Last Post: Dec 03, 2012, 11:31 AM
  3. [CLOSED] ViewBag value in combobox store listener [aspx]
    By boris in forum 2.x Legacy Premium Help
    Replies: 3
    Last Post: Oct 22, 2012, 7:44 AM
  4. [CLOSED] Access to viewbag using javascript code
    By Daly_AF in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: May 09, 2012, 9:02 PM
  5. Load ComboBox
    By usantos in forum 1.x Help
    Replies: 0
    Last Post: Mar 13, 2010, 12:29 PM

Tags for this Thread

Posting Permissions