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 7569471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:10:25+00:00 2026-05-30T15:10:25+00:00

Here is my situation: I have a controller action called OrderFromCategory which fills a

  • 0

Here is my situation:

I have a controller action called OrderFromCategory which fills a view model with data (view model in this case is just a list of items of type of another view model). This gets passed to the strongly typed view which displays the list of items and lets user change quantity of each item. On Post, my post controller action receives the model object back, but with no objects in the list. Am I miss-wiring something? At a loss, any help would be appreciated! Thanks much in advance!

Also, is my validation wired up correctly? Can’t test that until I get the model data to be persistent!

Here is the code:

ViewModel:

public class MenuItemsModel
{
    public MenuItemsOrderModel()
    {
        items = new List<MenuItemOrderModel>();
    }

    public List<MenuItemOrderModel> items { get; set; }
}

public class MenuItemOrderModel
{
    public int ItemID { get; set; }

    [Display(Name = "Name")]
    public string ItemName { get; set; }

    [Display(Name = "Description")]
    public string Description { get; set; }

    [Display(Name = "Price")]
    [DataType(DataType.Currency)]
    public decimal Price { get; set; }

    [Display(Name = "Quantity")]
    public int Quantity { get; set; }

    public string Qualifier { get; set; }
    public int Minimum { get; set; }
}

Controllers:

    public ActionResult OrderFromCategory(string id)
    {
        ViewData["category"] = id;
        Models.MenuItemsModel model = new Models.MenuItemsModel();

        foreach (Models.MenuItem item in Models.MenuItemRepository.GetMenuItemsForCategory(id))
        {
            model.items.Add(new Models.MenuItemOrderModel()
            {
                ItemID = item.ItemID,
                Description = item.Description,
                ItemName = item.Name,
                Price = item.Price,
                Qualifier = item.PriceQualifier,
                Minimum = item.MinimumQuantity,
                Quantity = ((namespace.Models.Order)Session["order"]).GetItemQuantity(item.ItemID)
            });
        }

        return View(model);
    }

    [HttpPost]
    public ActionResult OrderFromCategory(string id, Models.MenuItemsModel model)
    {
        //check for user inputs in all items
        foreach (string inputKey in Request.Form.AllKeys)
        {
            Models.MenuItem item = Models.MenuItemService.GetMenuItem(int.Parse(inputKey));
            int minimum = item.MinimumQuantity;
            int quantity = string.IsNullOrEmpty(Request.Form[inputKey]) ? 0 : int.Parse(Request.Form[inputKey]);

            if(quantity != 0 && quantity < minimum)
            {
                ModelState.AddModelError(string.Format("Quantity", item.ItemID), string.Format("Minimum of {0} required for order", minimum));
            }

            if (!ModelState.IsValid)
            {
                return View(model);
            }

            Models.OrderService.UpdateItemInOrder((Models.Order)Session["order"], item.ItemID, quantity);
        }

        return RedirectToAction("PlaceOrder");
    }

View:

@model namespace.Models.MenuItemsModel
@{
    ViewBag.Title = "Order from  " + ViewData["category"];
}

<h2>Order from @ViewData["category"] </h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>


@using (Html.BeginForm())
{
    @Html.ValidationSummary(true, "Order was unsuccessful. Please correct the errors and try again.")    
    <div>
        <fieldset>

        @foreach (namespace.Models.MenuItemOrderModel item in Model.items)
        {
            <div class="item_group">

              <div class = "item_name">
                @item.ItemName
              </div>
              <div class = "item_description">
                @item.Description
              </div>
              <div class = "item_price">
                $@item.Price /@item.Qualifier  (@item.Minimum miminum)
              </div> 
              <div class = "item_quantity">
                <div class="editor-label">
                    Quantity
                </div>
                <div class="editor-field">
                    @Html.TextBox(item.ItemID.ToString(), item.Quantity.ToString(), new { @class = "quantity_textbox", @type = "number"})
                    @Html.ValidationMessage("Quantity", "*")
                </div>
              </div>

            </div>
        }

        <p>
            <input type="submit" value="Continue" />
        </p>

        </fieldset>
   </div>
}
  • 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-30T15:10:27+00:00Added an answer on May 30, 2026 at 3:10 pm

    You don’t seem to have corresponding input fields in your form for the fields of your view model. Also your the naming of your quantity textbox is wrong. Try replacing your foreach loop in the view with the following:

    @for (int i = 0; i < Model.items.Count; i++)
    {
        @Html.HiddenFor(x => x.items[i].ItemID)
        @Html.HiddenFor(x => x.items[i].ItemName)
        @Html.HiddenFor(x => x.items[i].Description)
        @Html.HiddenFor(x => x.items[i].Price)
        @Html.HiddenFor(x => x.items[i].Qualifier)
        @Html.HiddenFor(x => x.items[i].Minimum)
    
        <div class="item_group">
            <div class = "item_name">
                @Model.items[i].ItemName
            </div>
            <div class = "item_description">
                @Model.items[i].Description
            </div>
            <div class = "item_price">
                $@Model.items[i].Price /@Model.items[i].Qualifier  (@Model.items[i].Minimum miminum)
            </div> 
            <div class = "item_quantity">
            <div class="editor-label">
                Quantity
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(x => x.items[i].Quantity, new { @class = "quantity_textbox", @type = "number" })
                @Html.ValidationMessageFor(x => x.items[i].Quantity, "*")
            </div>
            </div>
        </div>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the situation I have a webpage which has one drop down called prefer.
I have a strange situation in one view controller where this line crashes in
Here is the situation: I have been called upon to work with InstallAnywhere 8,
I have an AJAX controller action which returns JSON. The returned JSON is handled
I wanted to have more than one controller and view for same object/model in
The situation: Say I have an example controller called AccountController with some actions of
Here's my situation: i have a dialog form that inserts some data on my
I have a situation where I am sending data to a Controller via jQuery
Here is my situation - I have two nested view models: <%=Html.EditorFor(x => x.DisplayEntitiesWithRadioboxesViewModel)%><br
Here's the situation: I have a label's text set, immediately followed by a response.redirect()

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.