Steven
Steven

Reputation: 18859

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.

Upvotes: 0

Views: 158

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

You may checkout the following blog post.

Upvotes: 1

Related Questions