简体   繁体   中英

How to display the List of object to view

My Model Class

 public class DisplaceModel
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string Type { get; set; }
        public string Rating { get; set; }
        public string PhotoReference { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
    }

public class DisplaceModelInformation
    {
        public List<DisplaceModel> Dispaylist { get; set; }
        public DisplaceModelInformation()
        {
            Dispaylist = new List<DisplaceModel>();
        }
     }

And My Controller

var Display = new DisplaceModelInformation();
            XElement generalElement = xdoc1.Element("PlaceSearchResponse");
            Display.Dispaylist = (from c in xdoc1.Descendants("result")
                       select new DisplaceModel()
                        {
                            Name = Convert.ToString(c.Element("name").Value),
                            Address = Convert.ToString(c.Element("vicinity").Value),
                            Type = keyword,
                            Rating = (c.Element("rating") != null  ? Convert.ToString(c.Element("rating").Value) :null),
                            PhotoReference = (c.Element("photo") != null ? Convert.ToString(c.Element("photo").Element("photo_reference").Value) : null),
                            Width = (c.Element("photo") != null ? Convert.ToInt16(c.Element("photo").Element("width").Value) : 0),
                            Height = (c.Element("photo") != null ? Convert.ToInt16(c.Element("photo").Element("height").Value) : 0)
                        }).ToList<DisplaceModel>();

            return View(Display);

Now I try to display my list of object to view, I tried but I really dont know how to display in mvc view(cshtml) file

My MVC View

@model IEnumerable<FindLocation.Models.DisplaceModelInformation>

@using (Html.BeginForm())
{
    <table>
        <tbody>
            <tr>
                <th>NAME</th>
                <th>ADDRESS</th>
                <th>RATING</th>
            </tr>

            @foreach (var element in Model)
            {
                if (element.Dispaylist.Count > 0 )
            {
                <tr>
                    <@*td>@element.Dispaylist from c </td>
                    <td>@Display.Address</td>
                    <td>@Display.Rating</td>*@
                </tr>
            }
            }
        </tbody>
    </table>

I am new to UI and I try to extract the list of object but i dont know how to list the object. so please help me....and thanks for your help

Part of the trouble might be coming from the type difference between what you're passing to the view from the controller ( DisplaceModelInformation ) and the @model definition in the view itself ( IEnumerable<DisplaceModelInformation> ). Changing one to match the other should help.

I think you have the right idea with @foreach . If your model is an IEnumerable , you would need to iterate over that collection first, then iterate over the DisplayList collection in another inner loop.

The answers to the following question seem to have some good examples of what you're trying to accomplish: Foreach in a Foreach in MVC View

There are two issues in your code:

  1. In your Controller, you need to return View(new List<DisplaceModelInformation> { Display }); , because you specified an IEnumerable model in your View: @model IEnumerable<FindLocation.Models.DisplaceModelInformation>

  2. Use foreach to display each table row:

     @foreach (var element in Model) { if (element.Dispaylist.Count > 0) { foreach (var Display in element.Dispaylist) { <tr> <td>@Display.Name</td> <td>@Display.Address</td> <td>@Display.Rating</td> </tr> } } } 

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