简体   繁体   中英

Add data from two tables in one view MVC 4

I am trying to add data from two different table in one view. Here somne code: Code of viewmodel

public class ProductIndexData
{
    public IEnumerable<Order> Orders { get; set; }
    public IEnumerable<Magnet> Magnets { get; set; }
    public IEnumerable<Map> Maps { get; set; }
    public IEnumerable<Portrait> Portraits { get; set; }
    public IEnumerable<Tablet> Tablets { get; set; }
    public IEnumerable<Other> Others { get; set; }
}

Code of controller:

public ActionResult Index()
    {
        ProductIndexData products = new ProductIndexData();
        products.Magnets = (from o in db.Magnets select o).ToList();
        return View(products);
    }

Code of view:

@model List<SvLaserIS.ProductIndexData>
@{
    ViewBag.Title = "Product";
}

<h2>Product</h2>
<table>
@foreach (var item in Model) {
    foreach(SvLaserIS.Models.Magnet magnet in item) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => magnet.Count)
        </td>
        <td>
            @Html.DisplayFor(modelItem => magnet.Model)
        </td>
        <td>
            @Html.DisplayFor(modelItem => magnet.Color)
        </td>
        <td>
            @Html.DisplayFor(modelItem => magnet.FullPrice)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new {  }) |
            @Html.ActionLink("Details", "Details", new {  }) |
            @Html.ActionLink("Delete", "Delete", new {  }) 
        </td>
    </tr>
    }
}
    </table>

After compilation i ve got error in the second foreach cycle in view that says its not contait the definition for GetEnumerator. Looking for help with it :/

似乎您只是忘了指定要迭代item.Magnets属性:

foreach(SvLaserIS.Models.Magnet magnet in item.Magnets)

I solved it. Just passing single object in view and using single foreach cycle for each table in view. Thanks everyone for respones.

In model
--------

simple example 


 public class subdomain
   {

       public string Function { get; set; }
       public string SubDomain { get; set; }
       public string URL { get; set; }
       public Guid DomainId { get; set; }

   }
   public class emailserver
   {
       public string Priority { get; set; }
       public string MailExchanger { get; set; }
       public Guid DomainId { get; set; }
   }


  public class  tablevalues
   {
       public List<emailserver> emailserver { get; set; }

       public List<subdomain> subdomain { get; set; }
   }



in controller
-------------
tablevalues dto=new tablevalues();
dto.subdomain=from  p in context.tablename1  select p
dto.emailserver=from q in context.tablename2 select q
return View(dto);


in .cshtml
----------


 @model MVCDropDownlist.Models.tablevalues 

@{
    Layout = null;
}


 <table width="100%" border="0" cellspacing="0" cellpadding="0" id="tbsub" style="visibility: @(Model.subdomain.Count==0? "hidden" : "visible") " >
            <thead>
                <tr>
                    <td colspan="3">
                         <h1>Web Settings</h1>
                    </td>
                </tr>
                <tr>
                    <th>Web Sub Domain</th>
                    <th>Function</th>
                    <th>Points To</th>
                </tr>
            </thead>
            <tbody>
                @if (Model != null && Model.subdomain.Count() > 0)
                {
                    foreach (var item in Model.subdomain)
                    {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.SubDomain)
                        </td>
                        <td>@Html.DisplayFor(modelItem => item.Function)</td>
                        <td>@Html.DisplayFor(modelItem => item.URL)</td>
                    </tr>

                    }
                }
            </tbody>

        </table>
        <!--tableGrid -->
    </div>

    <!--rightSection -->
</div>

<div class="rightSection">

    <div class="tableGrid">
        <table width="100%" border="0" cellspacing="0" cellpadding="0" id="tbess" style="visibility: @(Model.emailserver.Count==0? "hidden" : "visible") ">
            <thead>
                <tr>
                    <td colspan="2">
                         <h1>Email Server Settings</h1>
                    </td>
                </tr>
                <tr>
                    <th>Mail Exchanger</th>
                    <th>Priority</th>
                </tr>
            </thead>
            <tbody>
                @if (Model != null && Model.emailserver.Count() > 0)
                {
                    foreach (var item in Model.emailserver)
                    {

                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.MailExchanger)
                        </td>
                        <td>@Html.DisplayFor(modelItem => item.Priority)</td>
                    </tr>

                    }
                }

            </tbody>

        </table>

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