Returning a dynamic list of items to my controller
I have a table in my database, named PriceRanges:
PriceRanges
-----------------
PriceRangeID (PK)
RangeFrom
RangeTo
Price
I have a page where a user can add multiple price ranges, set up like so:
[RangeFromTextbox] to [RangeToTextbox] - $[PriceTextbox]
[AddLink]
So when "Add" is clicked, another row appears, and they can enter another price range.
In my view model, I have:
IEnumerable<PriceRange> PriceRanges { get; set; }
In my controller, when the user submits the form I'd like to do something like this:
[HttpPost]
public ActionResult Edit(MyViewModel viewModel)
{
DBEntities entities = new DBEntities();
foreach (PriceRange priceRange in viewModel.PriceRanges)
{
entities.AddToPriceRanges(priceRange);
}
entities.SaveChanges();
}
But I don't know how to开发者_高级运维 relate a dynamic list of objects with my view model.
You may checkout the following blog post.
精彩评论