2

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; }
    }
user2471435
  • 1,644
  • 7
  • 35
  • 62
  • I had to do this not too long ago, create a hidden field, make it part of your model & add it as a hiddenfor field to your mvc form. Make your click function add / remove to your new hidden field and then your hidden field will have values and be passed as part of your model. A serializer goes a long way here towards code readability also (if your listbox deals w models especially). If you choose this approach, give it a shot & let us know if you get stuck or have any questions. – RandomUs1r Oct 23 '14 at 18:50
  • What is the actual problem. Is `SelectedAttributes2` not populated correctly in the post method? –  Oct 23 '14 at 22:22
  • And as a side note, your script can be simplified to one line of code: `$(this).val('L' + $(this).val()).appendTo("#listBoxSel")` –  Oct 23 '14 at 22:34
  • SelectedAttributes2 is populated correctly but I have it as a List and that gives me the string with the attribute value. I need the value that I put in the item. – user2471435 Oct 24 '14 at 13:51

1 Answers1

1

I don't fully understand your question, but from what I gather you're trying to pass a parameter to a function call in MVC. Parameters can be added to URLs much like in PHP.

For example a function could be defined as

void Function (string value);

and the URL to call it would be

~/Controller/Function?value=HelloWorld

You can also set up routing. See this answer on StackOverflow for more information:

ASP.NET MVC - passing parameters to the controller

Edit: I see you're trying to use Form submission

Make sure you correctly format your view, not forgetting :

@model Path.To.Model

and controller information:

using (@Html.BeginForm("myMethod", "Controller", FormMethod.Post))
Community
  • 1
  • 1
jProg2015
  • 1,098
  • 10
  • 40
  • No, I am not passing a parameter to a function call. I am simply trying to retrieve the Listbox's selected item's value that ALREADY exists after the Post – user2471435 Oct 23 '14 at 18:30
  • Sorry I understand now - You're trying to use Form submission. Changed my answer. – jProg2015 Oct 23 '14 at 18:38
  • That's not at all what I am asking. I'm not asking how to get to my post method. It gets there just fine. I am asking how to get the selected item's value in a listbox from the POST controller code. – user2471435 Oct 24 '14 at 13:49
  • If you're loading another page, pass it via another model into the new page and use it there. If you're on the same page, you can't. – jProg2015 Oct 24 '14 at 13:51
  • Look at the comments above about SelectedAttributes2. That's what the issue is. That gets filled by the ListBoxFor. I have that in the View model has List but that just returns the Selected Item text. I need to retrieve the value I put in there. – user2471435 Oct 24 '14 at 15:43