0
我想要在一個循環中查看所有列表值的視圖,這是我的控制器。有我定製的模型。我也嘗試過使用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>
}
我能讀我的數據?請幫助
thankx模型強烈結合的觀點,它的工作原理 –