lundi 23 mars 2015

Send listboxfor to controller

I have a form in my view that contains a listbox. The user adds values to the listbox.


When the user pushes the 'submit' button I want all the values in the listbox to be placed in my models property that is a List.


Here is the Listbox in my view:



@Html.ListBoxFor(m => m.Ingredients, new List<Ingredient>(), new { @class = "form-control" , @id = "Ingredients"})


This line gives an error that is that the List has to contain SelectListItem. Even if i change to that, and also change it in my view model, it still doesn't work.


This is my viewmodel:



public class RecipeModel
{
public int Id { get; set; }

public string Name { get; set; }

public List<Ingredient> Ingredients { get; set; }

public string Description { get; set; }

public string ImageUrl { get; set; }

public List<RecipeModel> Recipes { get; set; }
}


This is my controller:



[HttpPost]
public void SaveRecipe(RecipeModel model)
{
RecipeDb.SaveRecipe(Mapper.Map<Recipe>(model));
}


What I want is that in the controller, I want the models List Ingredients to be populated by all the items/values in the listbox from the view but I cant figure it out.


Aucun commentaire:

Enregistrer un commentaire