显示带有viewbag的对象内部的列表

时间:2016-05-20 19:14:52

标签: asp.net asp.net-mvc list viewbag

EXEMPLE:

public class country
    {
        public string name {get;set;}
        public string Alias{get;set;}
        public List<city> cities = new List<city>();

    }

当我在我的控制器上创建类国家的实例时...在视图中我只能使用ViewBag显示名称和别名。我的问题是如何使用带有foreach的Viewbag来访问和显示一个国家的所有城市。

这是我的真实情况..:

在模型中我有:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

    namespace StageApp.Models
    {
        public class DAT
        {
            public int IdDat {get;set;}
            public string AliasDat{get;set;}
            public string IdGare{get;set;}
            public List<Destinataires> destinataires = new List<Destinataires>();

        }
          public class DATBussinessLogic
          {
            public void addlistdesti(int iddat , DAT dat)
            {
                  String sql2 = "SELECT matricule,nom_destinataire,prenom_destinataire,etat FROM Destinataires where id_dat =" + dat.IdDat;
                  using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["StageConnection"].ConnectionString))
                  {
                      SqlCommand cmd2 = new SqlCommand(sql2, conn);
                      conn.Open();
                      SqlDataReader rdr2 = cmd2.ExecuteReader();
                      while (rdr2.Read())
                      {
                          var destis = new Destinataires();
                          destis.Matricule = rdr2["matricule"].ToString();
                          destis.NomDestinataire = rdr2["nom_destinataire"].ToString();
                          destis.PrenomDestinataire = rdr2["prenom_destinataire"].ToString();
                          destis.Etat = rdr2["etat"].ToString();
                          dat.destinataires.Add(destis);
                      }
                  }
            }
          }
    }

我有控制器:

using StageApp.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace StageApp.Controllers
{
    public class DATController : Controller
    {
        DATBussinessLogic DATBL = new DATBussinessLogic();

    public ActionResult Index()
        {
            if (Session["username"] == null)
            {

                return RedirectToAction("Index", "Home");
            }
            else
            {
                ViewBag.name = Session["username"];
                return View("Index");
            }
        }

        public ActionResult Listdat()
        {
            String sql = "SELECT * FROM dat";
            var model = new List<DAT>();

            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["StageConnection"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    var dat = new DAT();
                    dat.IdDat = (int)rdr["id_dat"];
                    dat.AliasDat = rdr["alias_dat"].ToString();
                    dat.IdGare = rdr["id_gare"].ToString();
                    dat.IdGare = DATBL.from_id_to_aliasgare(dat.IdGare);
                    DATBL.addlistdesti(dat.IdDat, dat); //a method from Models
                    model.Add(dat);

                }

            }
            ViewBag.dat = model;
            if (Session["username"] == null)
            {

                return RedirectToAction("Index", "Home");
            }
            else
            {
                ViewBag.name = Session["username"];
                return View("Listdat");
            }


        }
    }
}

这就是观点:

<div class="container body-content">
        <h2>Liste des DAT disponibles !!</h2>
        <table id="example" class="table table-striped table-bordered" cellspacing="0">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Alias</th>
                    <th>Gare</th>
                    <th>Déstinataires</th>

                </tr>
            </thead>
            <tbody>
                @foreach (var item in ViewBag.dat)
                {
                    <tr>
                        <td>@item.IdDat</td>
                        <td>@item.AliasDat</td>
                        <td>@item.IdGare</td>
                        <td>@foreach (var desti in ViewBag.dat.destinataires)
                            {
                                <span>Matricule : @desti.Matricule</span>  
                                <span>Nom et prénom : @desti.NomDestinataire @desti.PrenomDestinataire</span>
                                <span>Etat : @desti.Etat</span>
                                <span>***</span>
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>

你可以看到我试图显示Viewbag.dat的项目..工作正常但Viewbag.dat.destinataires不工作

我在ViewBag.dat.destinataires上遇到错误:

System.Collections.Generic.List&#39;不包含&#34; destinataires&#34;

的定义

任何想法都存在问题?

1 个答案:

答案 0 :(得分:0)

您正在ViewBag.dat圈内访问ViewBag.dat。您可以从destinataires获取item属性。

使用以下代码代替for循环。您必须将ViewBag.dat.destinataires更改为item.destinataires

 <td>@foreach (var desti in item.destinataires)
              {
                <span>Matricule : @desti.Matricule</span>  
                <span>Nom et prénom : @desti.NomDestinataire      
                @desti.PrenomDestinataire</span>
                 <span>Etat : @desti.Etat</span>
                <span>***</span>
             }
     </td>

它应该适合你。