PDA

View Full Version : [CLOSED] Issues getting MultiComboFor in MVC Razor to bind



AmitM
Nov 15, 2013, 3:38 AM
I was looking at this post:
http://forums.ext.net/showthread.php?23795-CLOSED-Sample-code-for-RadioFor-and-MultiComboFor-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

Daniil
Nov 15, 2013, 4:27 AM
Hi Amit,



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 }
});
}



Or, better yet, how would I select the values in the combo from the server?

Sorry, I don't understand the question. Please elaborate.

AmitM
Nov 15, 2013, 3:50 PM
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

Baidaly
Nov 15, 2013, 10:48 PM
Hello!


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.