我想在视图中查看值

时间:2016-05-29 15:39:25

标签: asp.net asp.net-mvc

我希望在循环中获取所有列表值,这是我的控制器。我已经制作了自定义模型。我也尝试过使用ViewData [' devieList']。但它对我不起作用。我从2天开始尝试这个。你的帮助会很感激。提前谢谢你

public ActionResult DeviceList()
    {
        SqlConnection conn = new SqlConnection(ConnectionString);
        conn.Open();
        STGY_SMAPEntities Db = new STGY_SMAPEntities();
        var model = new Clients();
        DeviceList deviceList = new DeviceList();


        model.Items = Db.CLIENT_MASTER.Select(x => new SelectListItem
        {
            Value = SqlFunctions.StringConvert((double)x.CLIENT_ID),
            Text = x.CLIENT_NAME
        }).ToList();

        foreach (var item in model.Items)
        {
            var devices = new Devices();
            devices.client_Name =  model.client_Name = item.Text;
            devices.client_ID =  model.client_ID = item.Value;
            SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM DEVICE_MASTER DM WHERE CLIENT_ID = " + model.client_ID, conn);
            SqlDataReader ds = null;
            ds = command.ExecuteReader();
            if (ds.HasRows)
                {
                    while (ds.Read())
                        {
                            devices.device_Count = ds.GetInt32(0);
                        }
                }
            deviceList.devicesList.Add(devices);
            ds.Close();
        }
        //IEnumerable<DeviceList> dev = DeviceList;
        ViewBag.deviceList = deviceList;
        return View(deviceList);// (deviceList);
    }

同时我得到这样的

@{int i = 0;}
                @foreach (var x in (List<string>)ViewData["deviceList"])
                {
                    i++;
                    <tr>
                        <td>
                            @*@x.CLIENT_ID*@
                            <span>@i</span>

                        </td>
                        <td>
                            @x.client_Name
                        </td>
                        <td>
                            @x.device_Count
                        </td>
                        <td>
                            @*<a href="@Url.Action(controllerName: "Home", actionName: "ViewDevices", routeValues: new { Id = @x.CLIENT_ID })"><img src="~/Content/images/copy.png" style="width: 20px" /></a>*@
                            @*<a href="@Url.Action(controllerName: "Home", actionName: "EditClients", routeValues: new { Id = @x.Clients_Id })" class="btn btn-info" role="button">Edit</a>
                                <a href="@Url.Action(controllerName: "Home", actionName: "DeleteClients", routeValues: new { Id = @x.Clients_Id })" class="btn btn-info" role="button">Delete</a>*@

                        </td>
                    </tr>
                }

我能够读取我的数据吗?请帮忙

1 个答案:

答案 0 :(得分:2)

由于您正在将模型传递给action方法中的视图,因此您应该能够通过强烈地将视图与模型绑定来实现此目的

@model IEnumerable<DeviceList>
@foreach (var x in Model)
                {
                   <tr>
                        <td>
                            @*@x.CLIENT_ID*@
                            <span>@i</span>

                        </td>
                        <td>
                            @x.client_Name
                        </td>
                        <td>
                            @x.device_Count
                        </td>
                        <td>
                            @*<a href="@Url.Action(controllerName: "Home", actionName: "ViewDevices", routeValues: new { Id = @x.CLIENT_ID })"><img src="~/Content/images/copy.png" style="width: 20px" /></a>*@
                            @*<a href="@Url.Action(controllerName: "Home", actionName: "EditClients", routeValues: new { Id = @x.Clients_Id })" class="btn btn-info" role="button">Edit</a>
                                <a href="@Url.Action(controllerName: "Home", actionName: "DeleteClients", routeValues: new { Id = @x.Clients_Id })" class="btn btn-info" role="button">Delete</a>*@

                        </td>
                    </tr>
                }