[CLOSED] Issues getting MultiComboFor in MVC Razor to bind

  1. #1

    [CLOSED] Issues getting MultiComboFor in MVC Razor to bind

    I was looking at this post:
    http://forums.ext.net/showthread.php...r-in-MVC-Razor

    and this link:
    http://mvc.ext.net/#/Models/Model_Bind/

    But I have not been able to get the MultiComboFor to show the values that are in the model.

    View:
    Html.X().MultiComboFor(m => m.EXTSecondaryActivities, false)
        .Icon(Model.IsLocked ? Icon.Lock : Icon.None)
        .ReadOnly(Model.IsLocked)
        .FieldLabel("Secondary Activity")
        .ValueField("ASID")
        .DisplayField("Description")
        .Store(Html.X().Store()
            .Model(Html.X().Model()
                .Fields(
                    new ModelField("ASID"),
                    new ModelField("Description")
                ).IDProperty("ASID")
            )
        .DataSource(Model.Activities)),
    Model:
    public IEnumerable<ListItem> EXTSecondaryActivities { 
                    get
                    {
                        return (from x in SecondaryActivities
                                select new ListItem()
                                {
                                    Value = x.ToString()
                                });
                    }
    
                    set
                    {
                        SecondaryActivities = (from x in value select Convert.ToInt32(x.Value)).ToArray();
                    }
                }
    Controller:
    var result = new Ext.Net.MVC.PartialViewResult()
                {
                    ViewName = "EditStudent",
                    ContainerId = containerId,
                    WrapByScriptTag = false,
                    Model = estudent,
                    RenderMode = Ext.Net.RenderMode.Replace,
                    IDMode = IDMode.Static,
                    ControlId = containerId
                };
    The model is inheriting from another class that has the multiple values in an int array. I have the EXTSecondaryActivities returning an IEnumerable<ListItem> as in the example. I would prefer to just use the int array so I would not have to inherit the base class (I could just use it directly).

    How would I get the MultiComboFor to bind correctly? Or, better yet, how would I select the values in the combo from the server?

    Thanks,
    Amit
    Last edited by Daniil; Nov 22, 2013 at 4:08 AM. Reason: [CLOSED]
  2. #2
    Hi Amit,

    Quote Originally Posted by AmitM View Post
    The model is inheriting from another class that has the multiple values in an int array. I have the EXTSecondaryActivities returning an IEnumerable<ListItem> as in the example. I would prefer to just use the int array so I would not have to inherit the base class (I could just use it directly).
    Is the following suitable for you?

    Model
    public class TestModel
    {
        public int[] SelectedItems { get; set; }
    
        public IEnumerable<ListItem> Items
        {
            get;
            set;
        }
    }
    View
    @model Work2MVC.Models.TestModel
    
    @{
        var X = Html.X(); 
    }
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Ext.Net.MVC v2 Example</title>  
    </head>
    <body>
        @X.ResourceManager()
    
        @{
            var selected = Model.SelectedItems.Select(item => new ListItem(item));
        }
    
        @X.MultiComboFor(m => selected).Items(Model.Items).FieldLabel("MultiCombo")
    </body>
    </html>
    Controller
    public ActionResult Index()
    {
        return View(new TestModel()
        {
            Items = new List<ListItem>()
            {
                new ListItem("Item 1", 1),
                new ListItem("Item 2", 2),
                new ListItem("Item 3", 3)
            },
    
            SelectedItems = new int[] { 1, 3 }
        });
    }
    Quote Originally Posted by AmitM View Post
    Or, better yet, how would I select the values in the combo from the server?
    Sorry, I don't understand the question. Please elaborate.
  3. #3
    I dont see how that will how that code will post the selected values back to the server when the user wants to save.

    Let me back up and explain the purpose. On this form, there are about 60 fields. One of which is "SecondaryActivities" which can have multiple values.

    The model is stored in an assembly seperate from the UI because it is shared with a webservice. That is why I have to inheret it to add any EXT specific lists (which is why I would prefer not to have ListItem).

    Previously, the Model had a property:
     public int[] SecondaryActivities { get; set; }
    And the multicombofor was bound to that and it would POST the array of selected ints just fine. However on the GET it would not show what items were selected when the view is generated (even though the values were in the array). So for this part of the question:
    Or, better yet, how would I select the values in the combo from the server?
    If I can set the selected items in the controller that would solve my issue (and I would not need to inheret the model). Something like:

    var result = new Ext.Net.MVC.PartialViewResult()
                {
                    ViewName = "EditStudent",
                    ContainerId = containerId,
                    WrapByScriptTag = false,
                    Model = estudent,
                    RenderMode = Ext.Net.RenderMode.Replace,
                    IDMode = IDMode.Static,
                    ControlId = containerId
                };
    
    //HOW TO DO THIS?
    result.MultiCombo.SelectedItems = ???

    So, perhaps the question should have been phrased "How do I set the MulitComboFor.SelectedItems in the Code Behind?" but still keep the binding on the POST?


    The other solution
    MultiComboFor(m => selected)
    will probably also work but I don't see how that will return the selecteditems to the POST method if the user makes changes.

    Thanks,
    Amit
    Last edited by AmitM; Nov 15, 2013 at 3:54 PM. Reason: wierdness with copy/paste
  4. #4
    Hello!

    Quote Originally Posted by AmitM View Post
    I dont see how that will how that code will post the selected values back to the server when the user wants to save.
    I don't quite understand why it should not send the values to the server? If this field within a FormPanel, its values will be sent to the server. This approach using Model is a standard way to send values from the Database to the Client.

    The model is stored in an assembly seperate from the UI because it is shared with a webservice. That is why I have to inheret it to add any EXT specific lists (which is why I would prefer not to have ListItem).
    You don't need to have ListItems in your class, you just have to store required IDs.

    If it's still not clear, could you please modify Daniil's sample to explain where do you experience this issue? Because it's difficult to get all points without a sample.

Similar Threads

  1. mvc razor bind contextmenu in buttons?
    By 924170040 in forum 2.x Help
    Replies: 1
    Last Post: Jul 22, 2013, 8:37 PM
  2. [CLOSED] Sample code for RadioFor and MultiComboFor in MVC Razor
    By MTSI in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Mar 06, 2013, 6:03 AM
  3. [CLOSED] [Razor] Add GridView to GridPanel in razor
    By boris in forum 2.x Legacy Premium Help
    Replies: 3
    Last Post: May 09, 2012, 4:23 PM
  4. [CLOSED] [Razor] Setup Auto load panel in razor
    By UnifyEducation in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Apr 27, 2012, 10:54 AM
  5. Replies: 1
    Last Post: Apr 09, 2012, 1:07 PM

Tags for this Thread

Posting Permissions