Link to home
Start Free TrialLog in
Avatar of Crystal Rouse
Crystal RouseFlag for United States of America

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
When you ran your code did it have errors or was something not working correctly?
Avatar of Crystal Rouse

ASKER

I don't get an error. Those form fields just do not appear.
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

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!
Not a problem, as always glad to help.