Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 488293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:41:33+00:00 2026-05-13T01:41:33+00:00

To give some background on my issue: I have 3 tables called Products, Packages,

  • 0

To give some background on my issue:

I have 3 tables called Products, Packages, and PackageContents. I can go in my application and add products, and then I can create packages out of them. One of the steps of setting up a package is to create all the package contents. To do this, I’ve created a view that runs through all of the products and sets up a new package content for each – placing the packageID and productID in hidden fields. Then the user can select a dropdown for each one to indicate if the product is in the package, or not.

Here’s my issue:

This renders a separate form for every product, each with it’s own save button. I’d like to have one submit button that saves all of the new package contents at once. Can someone give me an example of how to write my POST method to loop over and save all new package contents and how I would make a button in my view that saves all of them at once?

My Model:

public class PackageContentViewModel
{
    //Properties
    public IEnumerable<Product> products { get; set; }
    public Product product { get; set; }
    public Package package { get; set; }
    public PackageContent packageContent { get; set; }

    //Constructor
    public PackageContentViewModel(int id)
    {
        CustomerRepository customerRepository = new CustomerRepository();
        package = customerRepository.GetPackage(id);
        products = customerRepository.FindAllProducts().ToList();

        foreach (var product in products)
        {
            PackageContent packageContent = new PackageContent();
            {
                packageContent.PackageID = id;
                packageContent.ProductID = product.ProductID;

            }
        }
    }

The Controller:

public ActionResult AddContents(int id)
    {
        Package package = customerRepository.GetPackage(id);

        return View(new PackageContentViewModel(id) { });
    }

The View:

<% foreach (var product in Model.products)
   { %>

        <% using (Html.BeginForm())
           {%>

    <fieldset>
        <legend><%= Html.Encode(product.ProductName) %></legend>

            <%= Html.Hidden("PackageContentsID") %>
            <%= Html.ValidationMessage("PackageContentsID", "*") %>

            <%= Html.Hidden("PackageID", Model.package.PackageID) %>
            <%= Html.ValidationMessage("PackageID", "*") %>

            <%= Html.Hidden("ProductID", product.ProductID) %>
            <%= Html.ValidationMessage("ProductID", "*") %>


            <label for="InPackage">InPackage:</label>
            <%= Html.TextBox("InPackage") %>
            <%= Html.ValidationMessage("InPackage", "*") %>

            <label for="Restricted">Restricted:</label>
            <%= Html.TextBox("Restricted") %>
            <%= Html.ValidationMessage("Restricted", "*") %>
            <input type="submit" value="Create" />
    </fieldset>

    <% } %>

<% } %>

EDIT

I decided to post the code that ended up working for me, in case anyone else comes along and needs the same help:

My View:

<% int i = 0; using (Html.BeginForm("CreateContents", "Packages", new { id = Model.package.PackageID }))
    {

        foreach (var product in Model.products)
            { 

                %>
            <fieldset>
                <legend><%= Html.Encode(product.ProductName)%></legend>
                    <%= Html.Hidden("PackageContents[" + i + "].PackageContentsID")%>
                    <%= Html.ValidationMessage("PackageContentsID", "*")%>

                    <%= Html.Hidden("PackageContents[" + i + "].PackageID", Model.package.PackageID)%>
                    <%= Html.ValidationMessage("PackageID", "*")%>

                    <%= Html.Hidden("PackageContents[" + i + "].ProductID", product.ProductID)%>
                    <%= Html.ValidationMessage("ProductID", "*")%>

                    <label for="InPackage">InPackage:</label>
                    <%= Html.TextBox("PackageContents[" + i + "].InPackage", "yes")%>
                    <%= Html.ValidationMessage("InPackage", "*")%>

                    <label for="Restricted">Restricted:</label>
                    <%= Html.TextBox("PackageContents[" + i + "].Restricted", "no")%>
                    <%= Html.ValidationMessage("Restricted", "*")%>
            </fieldset>
            <%

        ++i; } %>

                    <input type="submit" value="Add Contents" />

<% } %>

My Controller:

public RedirectToRouteResult CreateContents(int id, IList<PackageContent> PackageContents)
    {
        Package package = customerRepository.GetPackage(id);

        foreach (var packageContent in PackageContents)
        {

            customerRepository.Add(packageContent);
            customerRepository.Save();

        }
        return RedirectToAction("SetPrice", new { id = package.PackageID });
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:41:33+00:00Added an answer on May 13, 2026 at 1:41 am

    Here you can read about binding to a list: Model binding to a list

    This article is old, in newer MVC version you don’t have to define <input type="hidden" name="products.Index" value="0" />, but the rest is valid.

    EDIT

    Answer to comment:

    If only thing that you do is select if product is in the package or not, you can use MultiSelectList.

    If you want to specify quantity or other attibutes, you can write:

    <% int i = 0; using (Html.BeginForm()) {%>
        <% foreach (var product in Model.products) { %>
            <label for="Restricted"><%= product.Name %></label>
            <%= Html.Hidden("products[" + i + "].ProductId",product.id) %>
            <%= Html.TextBox("products[" + i + "].Quantity",0) %>
            <br/>
       <% ++i; } %>
    <% } %>
    

    Post action:

    [HttpPost]
    public ActionResult Edit(IList<PackageContent> products)
    

    where PackageContent is

    class PackageContent
    {
        int ProductId{get;set;}
        int Quantity{get;set;}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone give me some info/background info on how I might go about writing
firstly I'll give some background regarding the situation. I have a website containing approximately
The Issue Just to give you some background, I'm currently enrolled as a student
And if you do, can you give some background information on the implementation and
To give some background, I am making a UINavigationControlled-based blog type app (I suppose
I would really like some advice here, to give some background info I am
Anybody please give some useful links on this topic.i need to create a content
What is the difference between <tiles:useAttribute ...> and <tiles:insertAttribute ...> Can you give some
Can some give me good jQuery lightbox plugin which has zoomin and zoomout function
I have an issue with this jquery code below. Please give me advice or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.