在MVC中,将数据ViewBag.value Count从Controller传递到View。

问题描述 投票:0回答:1

我有一个动作控制器返回List。动作控制器。

public ActionResult Details(int Num)
    {
        List<SelectListItem> centre_commercialItems = new List<SelectListItem>();
        using (SqlConnection con = new SqlConnection(@"Serv\SQLEXPRESS;database=DB;integrated security=yes"))
        {
            SqlCommand cmd = new SqlCommand("Details", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Num", Num);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
Centre_Commercial c = new Centre_Commercial();
                    c.Num_Centre = Convert.ToInt32(dr[1]);
                    c.budget = float.Parse(dr[2].ToString());
                    c.Nombre_etage = Convert.ToInt32(dr[3].ToString());
                    ViewBag.NbreActivite = Convert.ToInt32(dr[0].ToString());
                    ViewBag.NbreLocaux = Convert.ToInt32(dr[4].ToString());

                    centre_commercial.Add(c);

            }
            con.Close();

        }
            ViewBag.Centre_Commercial = centre_commercialItems;
ViewBag.NbreActivite;ViewBag.NbreLocaux
here i get error viewBag.NbreActivite not in class just count from requete by storedprocedure how can i pass these two value from controller to view 
            return View();
        }

我想得到这个列表的值selectItem的视图与asp.net MVC当我这样做,我没有任何结果在下面的代码的视图,所以两个记录只是计算的计数,如A和L不存在的模型类,我想显示在视图也。

    @model ControlWCSNew.Models.Centre_Commercial
@{
    ViewBag.Title = "Details";
}

<h2>Details du Centre</h2>
<table class="table table-bordered table-striped">
    <thead>
        <tr>

            <th>@Html.DisplayNameFor(ViewBag.Centre_Commercial.Num_Centre)</th>
            <th>@Html.DisplayNameFor(ViewBag.Centre_Commercial.budget)</th>
            <th>@Html.DisplayNameFor(ViewBag.Centre_Commercial.Nombre_etage)</th>
            <th>@Html.DisplayNameFor(ViewBag.Centre_Commercial.L)</th>
            <th>@Html.DisplayNameFor(ViewBag.Centre_Commercial.A)</th>

        </tr>
    </thead>
    <tbody>

        @{
            foreach (var item in Model)
            {
                <tr>

                    <td>@Html.DisplayFor(modelItem => item.Num_Centre)</td>
                    <td>@Html.DisplayFor(modelItem => item.budget)</td>
                    <td>@Html.DisplayFor(modelItem => item.Nombre_etage)</td>
                    <td>@Html.DisplayFor(modelItem => item.L)</td>
                    <td>@Html.DisplayFor(modelItem => item.A)</td>
                </tr>

            }
        }
    </tbody>
</table>

先谢谢了

asp.net model-view-controller view controller action
1个回答
0
投票

Razor Pages中没有ViewBag。

你可以尝试用 ViewModel 来代替。

 return View(Centre_Commercial);

如果你想把Centre_Commercial,NbreActivite,NbreLocaux从Controller传递给View,你应该建立一个新的类来获取这三个属性。

详情请看下面的链接。https:/docs.microsoft.comzh-cnaspnetcoremvcviewsoverview?view=aspnetcore-3.1。

© www.soinside.com 2019 - 2024. All rights reserved.