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.