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.
c# forms asp.net-mvc-4
add a comment |
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.
c# forms asp.net-mvc-4
add a comment |
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.
c# forms asp.net-mvc-4
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.
c# forms asp.net-mvc-4
c# forms asp.net-mvc-4
edited Mar 23 '15 at 15:53
RedLaser
5801618
5801618
asked Mar 23 '15 at 15:45
Paul Chefen LagmarkPaul Chefen Lagmark
45118
45118
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
A multiple select post back an array of simple values (say [1, 4, 7]
or ["Recipe 2", "Recipe 4" ]
depending on the values of the selected options). You cannot bind ListBoxFor
to a collection of complex objects. Your view model needs to have a property to bind to. Assuming typeof Ingredient
contains properties int ID
and string Name
, then you view model would be something like
public class RecipeViewModel
{
public int SelectedIngredients { get; set; }
public SelectList IngredientList { get; set; }
}
Then the controller
public ActionResult Create()
{
RecipeViewModel model = new RecipeViewModel();
// Populate the collection of available ingredients
model.IngredientList = new SelectList(db.Ingredients, "ID", "Name");
// if you want to pre select some options, then: model.SelectedIngredients = new int { 1, 4, 7 };
return View(model);
}
[HttpPost]
public ActionResult Create(RecipeViewModel model)
{
// model.SelectedIngredients will contain the ID's of the selected ingredients
}
And in the view
@model RecipeViewModel
@using(Html.BeginForm())
{
....
@Html.ListBoxFor(m => m.SelectedIngredients, Model.IngredientList)
....
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29214479%2fsend-listboxfor-to-controller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A multiple select post back an array of simple values (say [1, 4, 7]
or ["Recipe 2", "Recipe 4" ]
depending on the values of the selected options). You cannot bind ListBoxFor
to a collection of complex objects. Your view model needs to have a property to bind to. Assuming typeof Ingredient
contains properties int ID
and string Name
, then you view model would be something like
public class RecipeViewModel
{
public int SelectedIngredients { get; set; }
public SelectList IngredientList { get; set; }
}
Then the controller
public ActionResult Create()
{
RecipeViewModel model = new RecipeViewModel();
// Populate the collection of available ingredients
model.IngredientList = new SelectList(db.Ingredients, "ID", "Name");
// if you want to pre select some options, then: model.SelectedIngredients = new int { 1, 4, 7 };
return View(model);
}
[HttpPost]
public ActionResult Create(RecipeViewModel model)
{
// model.SelectedIngredients will contain the ID's of the selected ingredients
}
And in the view
@model RecipeViewModel
@using(Html.BeginForm())
{
....
@Html.ListBoxFor(m => m.SelectedIngredients, Model.IngredientList)
....
}
add a comment |
A multiple select post back an array of simple values (say [1, 4, 7]
or ["Recipe 2", "Recipe 4" ]
depending on the values of the selected options). You cannot bind ListBoxFor
to a collection of complex objects. Your view model needs to have a property to bind to. Assuming typeof Ingredient
contains properties int ID
and string Name
, then you view model would be something like
public class RecipeViewModel
{
public int SelectedIngredients { get; set; }
public SelectList IngredientList { get; set; }
}
Then the controller
public ActionResult Create()
{
RecipeViewModel model = new RecipeViewModel();
// Populate the collection of available ingredients
model.IngredientList = new SelectList(db.Ingredients, "ID", "Name");
// if you want to pre select some options, then: model.SelectedIngredients = new int { 1, 4, 7 };
return View(model);
}
[HttpPost]
public ActionResult Create(RecipeViewModel model)
{
// model.SelectedIngredients will contain the ID's of the selected ingredients
}
And in the view
@model RecipeViewModel
@using(Html.BeginForm())
{
....
@Html.ListBoxFor(m => m.SelectedIngredients, Model.IngredientList)
....
}
add a comment |
A multiple select post back an array of simple values (say [1, 4, 7]
or ["Recipe 2", "Recipe 4" ]
depending on the values of the selected options). You cannot bind ListBoxFor
to a collection of complex objects. Your view model needs to have a property to bind to. Assuming typeof Ingredient
contains properties int ID
and string Name
, then you view model would be something like
public class RecipeViewModel
{
public int SelectedIngredients { get; set; }
public SelectList IngredientList { get; set; }
}
Then the controller
public ActionResult Create()
{
RecipeViewModel model = new RecipeViewModel();
// Populate the collection of available ingredients
model.IngredientList = new SelectList(db.Ingredients, "ID", "Name");
// if you want to pre select some options, then: model.SelectedIngredients = new int { 1, 4, 7 };
return View(model);
}
[HttpPost]
public ActionResult Create(RecipeViewModel model)
{
// model.SelectedIngredients will contain the ID's of the selected ingredients
}
And in the view
@model RecipeViewModel
@using(Html.BeginForm())
{
....
@Html.ListBoxFor(m => m.SelectedIngredients, Model.IngredientList)
....
}
A multiple select post back an array of simple values (say [1, 4, 7]
or ["Recipe 2", "Recipe 4" ]
depending on the values of the selected options). You cannot bind ListBoxFor
to a collection of complex objects. Your view model needs to have a property to bind to. Assuming typeof Ingredient
contains properties int ID
and string Name
, then you view model would be something like
public class RecipeViewModel
{
public int SelectedIngredients { get; set; }
public SelectList IngredientList { get; set; }
}
Then the controller
public ActionResult Create()
{
RecipeViewModel model = new RecipeViewModel();
// Populate the collection of available ingredients
model.IngredientList = new SelectList(db.Ingredients, "ID", "Name");
// if you want to pre select some options, then: model.SelectedIngredients = new int { 1, 4, 7 };
return View(model);
}
[HttpPost]
public ActionResult Create(RecipeViewModel model)
{
// model.SelectedIngredients will contain the ID's of the selected ingredients
}
And in the view
@model RecipeViewModel
@using(Html.BeginForm())
{
....
@Html.ListBoxFor(m => m.SelectedIngredients, Model.IngredientList)
....
}
answered Mar 23 '15 at 23:40
user3559349
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29214479%2fsend-listboxfor-to-controller%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown