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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:17:23+00:00 2026-06-08T05:17:23+00:00

I am new to MVC. i have problem to pass data from view to

  • 0

I am new to MVC. i have problem to pass data from view to controller.

This application is for ordering Shirts. I have different types like T-shirts, Casual shirts and Formal Shirts. In a single order, I would like to place order for 2 T-shirts, 3 Casual shirts and 5 Formal Shirts. I am able to edit single value, i.e. either of them, in one order. The problem I am facing is with ordering multiple types of shirts in one order.

public class Shirt    
{
   //[Key]
   public int ShirtId{ get; set; }
   public string ShirtName{ get; set; } // This is an read-only field    
}

public class Order
{
    public int ShirtId { get; set; }
    public virtual Shirt Shirt { get; set; } // a reference to Shirt
    public int Quantity { get; set; } 
    public int OrderId { get; set; }
}

I have a view that is strongly typed with my viewmodel “Order”. Initially I passed data from my Controller to View using

     [HttpGet]
    public ActionResult Index()
    {
       OrdersList();
       return View(db.Order.ToList());
    }

Here OrdersList is a function that interacts with database and gets a list of Orders

   void OrdersList()
   {
     ViewBag.OrdersLsit= from f in db.Shirt
                            orderby f.ShirtName
                            select f;
   }

I am getting data back to Controller using,

    [HttpPost]
    public ActionResult Index(List<Order> order)
    {
        Order newOrder = new Order();
        for (int i = 0; i < order.Count; i++)
        {
            newOrder.Quantity = order.ElementAt(i).Quantity;
            if (ModelState.IsValid)
            {
                db.Entry(newOrder).State = EntityState.Modified;

                db.SaveChanges();
            }
        }
    }

My problem is List order is passed as Null. If I do
public ActionResult Index(Order order) in HttpPost, I am getting only the first item(in this case a T shirt is getting updated in database).

In Index.cshtml, I am doing something like this

  <legend>Order Shirts</legend>
         <table>
            <tr>
                @{List<Order> orderList = new List<Order>(); }
                @foreach (Order ord in ViewBag.OrderList)
                {
                    <td>
                    @Html.DisplayFor(model => ord.Shirt.Shirtname)
                    </td>
                    orderList.Add(ord);
                }
            </tr>

            <tr>
                @for (int i = 0; i < orderList.Count; i++)
                {
                    <td class = "editorClass">
                            @Html.HiddenFor(model => orderList.ElementAt(i).Shirt.ShirtId)
                            @Html.HiddenFor(model => orderList.ElementAt(i).OrderId)
                            @Html.ValidationSummary(true)
 @Html.HiddenFor(model => orderList.ElementAt(i).ShirtId)                                
                            @Html.EditorFor(model => orderList.ElementAt(i).Quantity)
                            @Html.ValidationMessageFor(model => orderList.ElementAt(i).Quantity)
                    </td>
                }
            </tr>
        </table>
    </fieldset>
   <p>
       <input id = "btnSubmit" type="submit" value="Save Order" /> 
    </p>
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

I am new to MVC and this is my first application. Any constructive comments and suggestions are appreciated.
Thanks

  • 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-06-08T05:17:25+00:00Added an answer on June 8, 2026 at 5:17 am

    I don’t understand why you are creating orderList instead the view, actually you have to pass that from the controller and strongly type the view to List<Order>.

    Instead of using this,

    @Html.EditorFor(model => orderList.ElementAt(i).Quantity)
    

    You could try (if you strongly type the view to List<Order>),

    @Html.EditorFor(m => m[i].Quantity)
    

    If the view is not strongly typed to List<Order> you could try this,

    @Html.EditorFor(m => orderList[i].Quantity)
    

    But in this case you have to change the action as below,

    [HttpPost]
    public ActionResult Index([Bind(Prefix="orderList")]List<Order> order)
    {
    }
    

    The reason is I’m not sure whether the ElementAt(i) generate the names properly for fields, so try index approach!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my MVC application I have a problem with passing data from view to
Have problem while getting data from Memcached on .NET MVC solution. I have this
I am new to MVC Razor and I have this problem Ex: I want
New to MVC, so hopefully this will be pretty simple. I have an HTML
I am just new in MVC 3. I have a html view which contain
I currently have a View in MVC where I pass a single set of
This is my problem, I have a tiny PHP MVC framework i built. Now
I have a simple ASP.NET MVC 3 site, from my Controller out of the
I am fairly new to MVC. I have a simple problem. I have 3
I have the problems with pass list of string into controller in mvc 3

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.