简体   繁体   中英

How to add items in the ListView in MVC project from controller

I have seen many websites which show the way to do it is :

<ul>
@foreach (var customer in Model.Customer)
{
    <li>@customer</li>
} 
</ul>

What i'm trying to accomplish is to have a listview which adds data to it from an XML file.

I have done the same thing on a windows form whos code behind looks like:

            XElement xRoot = ManagerHelper.XMLManager().LoadOrderXML();

            var ordermodel = new OrderModel(xRoot).orders.List;
            lstbxClientNameNo.Items.Clear();
            foreach (var order in ordermodel)
            {
                lstbxClientNameNo.Items.Add(string.Format("{1} - {0}", 
                order.clientName, order.orderNumber));
            }

however could someone guide me to do the same/similar thing on an MVC project where I can pull the data from Model in View.

I have copied the model over to MVC from windows form and I wish to use the same data. The reason I'm building the same application on MVC is because I'm trying to learn MVC and how it works.

My model looks like:

public XElement LoadOrderXML()
        {
            XmlDocument xmldoc = new XmlDocument();

            xmldoc.Load(Environment.GetFolderPath(MYFORLDERPATH));
            XElement xRoot = XElement.Parse(xmldoc.InnerXml);
            return xRoot;




  public class OrderModel
    {
        public Orders orders;

        public OrderModel(XElement xOrder)
        {
            orders = new Orders(xOrder);         
        }

        public class Order
        {
            public string orderNumber, clientName;
            public decimal totalOrderPrice;
            public Products products;

            public Order()
            {
            }

            public Order(XElement xOrder)
            {
                XElement xClientNumber = xOrder.Element("OrderNumber");
                XElement xClientName = xOrder.Element("ClientName");
                XElement xTotalOrderPrice = xOrder.Element("TotalOrderPrice");

                XElement xProduct = xOrder.Element("Products");

                if (xClientNumber == null) return;
                orderNumber = xClientNumber.Value;
                clientName = xClientName.Value;
                totalOrderPrice = decimal.Parse(xTotalOrderPrice.Value);
                products = new Products(xProduct);
            }

            public XElement ToXML()
            {
                var xOrder = new XElement("Order");

                var xClientNumber = new XElement("OrderNumber", orderNumber);
                var xClientName = new XElement("ClientName", clientName);
                var xTotalOrderPrice = new XElement("TotalOrderPrice", 
                                                    totalOrderPrice);
                var xProducts = new XElement("Products", products);
                xOrder.Add(xClientNumber);
                xOrder.Add(xClientName);
                xOrder.Add(xTotalOrderPrice);
                xOrder.Add(xProducts);

                return xOrder;
            }
        }

        public class Orders
        {
             private readonly List<Order> _list;
            public List<Order> List { get { return _list; } }

            public Orders(XElement xAddresses)
            {
                _list = new List<Order>();

                var xProductList = xAddresses.Descendants().ToList();

                foreach (XElement ele2 in xAddresses.Descendants("Order"))
                {
                    _list.Add(new Order(ele2));
                }
            }        
        }

Based on your comments and code, I think that what you are looking for is this:

<ul>
@foreach (Order order in Model.orders.List)
{
    <li>@order.clientName - @order.orderNumber</li>
} 
</ul>

This assumes that your are sending an OrderModel object as the view model:

public ActionResult MyOrderAction()
{
    //get the XML
    XElement xmlData = ReadTheXmlData();

    //get the model
    OrderModel model = new OrderModel(xmlData);

    return View(model);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM