我有一個類,像這樣:ASP.NET通模式來查看
public class wsvListTagHistoryClass
{
public int TagId { get; set; }
public int GroupId { get; set; }
public string GroupName { get; set; }
public string Entered { get; set; }
public string DHours { get; set; }
public string GPS { get; set; }
}
,在我的視圖控制器,我填充類,像這樣:
int TagId = Convert.ToInt32(Request.QueryString["TagId"]);
wsvListTagHistoryCell = new List<wsvListTagHistoryClass>();
try
{
//Create SQL Connection
using (connection = new SqlConnection(connectionString))
{
//Open Connection
connection.Open();
//Run iosShowGroup Stored Procedure
using (SqlCommand command = new SqlCommand("wsvListTagHistory", connection))
{
command.CommandType = CommandType.StoredProcedure;
//Input Parameters
SqlParameter parameter1 = new SqlParameter("@TagId", SqlDbType.Int);
parameter1.Value = TagId;
parameter1.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter1);
//Execute Query
using (SqlDataReader reader = command.ExecuteReader())
{
//Get Group Data
while (reader.Read())
{
//Create New Class Object
wsvListTagHistoryClass item = new wsvListTagHistoryClass();
//Populate Object with Results
item.TagId = (int)reader[0];
item.GroupId = (int)reader[1];
item.GroupName = reader[2].ToString();
item.Entered = reader[3].ToString();
item.DHours = reader[4].ToString();
item.GPS = reader[5].ToString();
//Add Results to List
wsvListTagHistoryCell.Add(item);
}
}
}
}
}
finally
{
//Close Connection
connection.Close();
}
return View(wsvListTagHistoryCell.ToList());
,然後嘗試做在我看來,一個foreach像這樣:
@model IEnumerable<WebServiceAPI.Models.wsvListTagHistoryClass>
@{
ViewBag.Title = "Index";
}
@foreach(var item in Model)
{
@Html.DisplayFor(modelItem => item.TagId)
}
<h2>Index</h2>
我沒有錯誤,但我的問題是,出現任何內容,標籤識別不會出現在所有。我究竟做錯了什麼?
最好嘗試exec的在SQL管理工作室等你storeprod檢查是否有輸出(TagId) –
在返回視圖行上設置一個斷點,以查看您是否實際返回任何內容,如果是,請在視圖中的@foreach上設置斷點以查看它正在執行的操作。 將它運行後,將該Sql功能移到另一個類中,保持您的控制器精益和清潔是一種好的做法。 – MartijnK
可能是視圖中的foreach中的拼寫錯誤,但它爲什麼是modelItem => item.TagId?它不應該是modelItem => modelItem.TagId? – Markus