I have an app which has two listboxes:
@using (Html.BeginForm())
{
@Html.ListBoxFor(m => m.SelectedAttributes, Model.Attributes, new {id="listBoxAvail", SIZE = 5} )
<button type="button" id="add">MoveRight</button>
<button type="button" id="remove">"MoveLeft"></button>
<button type="button" id="remove-all">RemAll</button>
<button type="button" id="select-all">SelAll</button>
<button type="button" id="up" onclick="MoveUp()">Up</button>
<button type="button" id="down" onclick="MoveDown()">Down</button>
@Html.ListBoxFor(m => m.SelectedAttributes2, Model.SelectedItems, new { id = "listBoxSel", SIZE = 5})
<br />
<p>Item Caption Text (140 Characters. HTML not allowed.)</p>
@Html.TextAreaFor(m => m.ItemCaptionText)
<input type="submit" id="submit" value="Save"/>
}
I transfer the selected values from one listbox to the other using jQuery but I append an "L" to the value for later use:
$("#add").click(function () {
$("#listBoxAvail > option:selected").each(function () {
$(this).remove();
val = $(this).val();
temp = $(this);
temp.val('L' + val)
$(temp).appendTo("#listBoxSel");
});
});
I see the Text and Value after I transfer to the right hand listbox with the F12 Developer Tools. What I would to know is how to get that value from C# code in the Controller?
[HttpPost]
public ActionResult Index(OptInViewModel viewModel)
{
AttributeEntities db = new AttributeEntities();
IEnumerable<string> items = viewModel.SelectedAttributes2;
foreach (var item in items)
{
var temp = item;
// Save it
SelectedHarmonyAttribute attribute = new SelectedHarmonyAttribute();
attribute.CustomLabel = viewModel.ItemCaptionText;
//attribute.IsVisible = ?
//attribute.OrderNumber = ?
//attribute.HarmonyAttribute_ID
}
return View("Index");
}
The ViewModel is:
public class OptInViewModel
{
public IEnumerable<string> SelectedAttributes { get; set; }
public IEnumerable<string> SelectedAttributes2 { get; set; }
public IEnumerable<SelectListItem> Attributes { get; set; }
public IEnumerable<SelectListItem> SelectedItems { get; set; }
public string ItemCaptionText { get; set; }
}