1

I have a MVC 3 project, and is stuck on the binding of the view back to the model on Post.

Here is my Model:

public class DBViewModel
{
   public ProductionDatabasesViewModel PDBViewModel { get; set; }
   public IList<string> SelectedNames { get; set; }

   public DBViewModel()
   {
     SelectedNames = new List<string>();
     PDBViewModel = new ProductionDatabasesViewModel();
   }
}

My view: (cut down version)

@model Web.Models.DBViewModel
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
       @Html.ListBoxFor(x => x.SelectedNames, new SelectList(Model.SelectedNames))
       <input id="button-submit" name="SubmitButton" type="submit" value="Submit" />
    </fieldset>
}

Snippet of the Controller:

  [HttpPost]
  public ActionResult Database(DBViewModel model)
  {
    var users = model.SelectedNames; //<---- empty, the count is 0 always.
  }

In the view there is the ability to select users from a autocomplete box and then jQuery script that will add the selected names to the list in the view above. This all works, so I can search for the users, and then click the add button to add the users to the list. But I run into problems when clicking the Submit button. The users that were added in the view (selectlist in the view), is not bound to the Model. So on post the model.SelectedNames is empty. I have also tried Request["SelectedNames"] and Request.Form["SelectedNames"] and they are all null.

I am very experienced in normal webforms, but still learning MVC, so any help would be much appreciated.

[UPDATE]

I will update the question further tomorrow my time, but it appears that the items that is in the list will be bound to the viewmodel if I select them. I will figure this out tomorrow. But thanks a lot so far for all the comments, help and suggestions.

Ryk
  • 3,072
  • 5
  • 27
  • 32
  • I think you want something like this http://stackoverflow.com/questions/3194143/challenges-with-selecting-values-in-listboxfor/3195986#3195986 – Ryan May 25 '11 at 03:02
  • @Ryan - no unfortunately not. That does not solve the issue of binding the form values back to the Model during a post. – Ryk May 25 '11 at 03:31

3 Answers3

3

Does this Does the Model Binder in ASP.NET MVC Beta Support List<T>? answer your question? The string list is a simpler version.

Community
  • 1
  • 1
37Stars
  • 2,489
  • 20
  • 23
  • Has this not changed for MVC 3? as in easier, better supported? – Ryk May 25 '11 at 06:41
  • Also see comment I made below to Alexander. I dont have a problem programatically loading data and then getting it back during post, it seems that when I "insert" new values to the HTML select list using jQuery, that I cannot get the values. Does this makes sense? – Ryk May 25 '11 at 06:47
1

Have to say this cut off version of your code works as it should. SelectedNames list is successfully filled from listbox after submit. You can check it yourself. Though I have made several changes:

  • removed all references to ProductionDatabasesViewModel;
  • changed return type of POST version of Database method to void;
  • added GET version of Database method

:

public ActionResult Database()
{
    return View(new DBViewModel { SelectedNames = { "a", "b", "c" } });
}

So I believe you have a bug somethere else, not in these parts.

Alexander Prokofyev
  • 33,874
  • 33
  • 95
  • 118
  • yes that works, even for me, but my problem is not when I programatically create lists and add it, it seems that the problem is that the list is empty at load time, then I dynamically add entries to is using jQuery. So when I submit the form then, I cannot find the values. – Ryk May 25 '11 at 06:45
  • Could you refine your question and show how you add entries to the list with jQuery? – Alexander Prokofyev May 25 '11 at 06:50
  • Ok, will do, but I think I have come across the issue, and I will update my question. The problem seems that when I add a new SelectList item, it will only add it to the post binding, if they are selected. I will update it tomorrow, but thanks a lot for the help so far. – Ryk May 25 '11 at 11:15
1

Ok, I have found what the issue is. When you have a multi select element in your html that you want to bind the values in the box back to the model, you have to select the items first. Very annoying, but that's the way it seems to want it.

So if you have a select box like:

<select id="selected-ad-users-list" name="SelectedNames" multiple="multiple" size="5"
       style="width: 100%">
</select>

And you add items to it, from anywhere really, in my case I had another list/or textbox where they typed a name in, and then I used jQuery to populate this select list. What I needed to do was to add a bit of jQuery script to spin through my list and select all items. So once the items are selected, it will bind them to the model.

Here is a good article on this: select all or some items in a select box

I really hope this helps someone.

Ryk
  • 3,072
  • 5
  • 27
  • 32