How to send a List to the View from the controller.

I have a form that I want to populate two form fields with data.  The user clicks on an button on a details view and is sent to a Form.  I pass the id as a session.

Here is my controller:

 [HttpGet]
        public ActionResult CreateOrder()
        {
            int id = Convert.ToInt32(HttpContext.Session["Id"]);
            var Name = "";
            var Desc = "";
           
            var data = DB.vw_Order
                            .Where(x => x.ID == id
                             ).Single();

            Name = data.Name;
            Description = data.Description;
   
           newOrder newOrd = new newOrder()
            {
                OrderDate = DateTime.Now,
                Nomenclature = nomen,
                Name = name,
           };
 
            return View(newOrd);
        }

Here is a snippet of my form:

 <div class="col-xs-4">
                                @Html.Label("Name")<br />
                                @Html.TextBox("Name", Model.Name, new
                           {
                               @disabled = true,
                               @class = "form-control"
                           })
 </div>

The above works.  Now I need to populate two other fields but the returned data could be more than one record.

Can I add a list to my controller and pass that to the View and then use For Loop?
Crystal RouseTask LeadAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Crystal;

Yes that can be done. Just modify the newOder class to have the list populate it and send it off to the view to be used in your for loop.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
When you ran your code did it have errors or was something not working correctly?
Crystal RouseTask LeadAuthor Commented:
I don't get an error. Those form fields just do not appear.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Fernando SotoRetiredCommented:
Hi Crystal;

Here is some code I put together quickly and tested before posting and does work. When you modify your code to something as follows and if it does not work please let me know with some details.
// The Model

namespace WebApplication1.Models
{
    public class newOrder
    {
        public newOrder() { MyItems = new List<OrderData>(); }

        public DateTime OrderDate;
        public String Name;
        public String Location;
        public List<OrderData> MyItems;
    }

    public class OrderData
    {
        public String Name;
        public Int32 Quantity;
    }
}

// The Controller

namespace WebApplication1.Controllers
{
    public class OrderController : Controller
    {
        // GET: Order
        public ActionResult Order()
        {
            var orders = new newOrder();
            orders.Name = "Order1";
            orders.OrderDate = DateTime.Now;
            orders.Location = "A1-R2";
            (orders.MyItems).Add(new OrderData { Name = "Laptop", Quantity = 10 });
            (orders.MyItems).Add(new OrderData { Name = "Monitor", Quantity = 12 });


            return View(orders);
        }
    }
}

// The View

@model WebApplication1.Models.newOrder

<div>Name :  @Model.Name</div>
<div>Order Date : @Model.OrderDate.ToString("MMM dd, yyyy")</div>


@foreach( var item in Model.MyItems )
{
    <div>Name     : @item.Name</div>

    <div>Quantity : @item.Quantity</div>
}

Open in new window

Crystal RouseTask LeadAuthor Commented:
I actually got it working.  It ended up being a div on a table row that had been set to display = none.    Thanks so much for your help!
Fernando SotoRetiredCommented:
Not a problem, as always glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.