【发布时间】:2011-07-01 16:09:42
【问题描述】:
由于某种原因,我的 Html 助手无法识别。
@using System.Data.SqlClient
@using System.Data
<!DOCTYPE html>
<html>
<head>
<title>Site Visits</title>
</head>
<body>
<div>
@{
public string GetSiteVisits()
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(
"SELECT numVisits FROM tblSiteVisits WHERE IPAddress='" + Request.UserHostAddress + "'",
new SqlConnection("Data Source=*****;Initial Catalog=*****;Persist Security Info=True;User ID=*****;Password=*****;MultipleActiveResultSets=True"));
sda.Fill(dt);
string table = "<table><tr>";
foreach (DataColumn dc in dt.Columns)
{
table += "<th>" + dc.ColumnName + "</th>";
}
table += "</tr>";
foreach (DataRow dr in dt.Rows)
{
table += "<tr>";
foreach (Object o in dr.ItemArray)
{
table += "<td>" + o.ToString() + "</td>";
}
table += "</tr>";
}
table += "</table>";
return table;
}
}
<div>
@Html.Raw(GetSiteVisits())
</div>
</div>
</body>
</html>
有人知道怎么解决吗?
【问题讨论】:
-
为什么要在视图中执行模型工作?
-
如果您不打算遵循该模式,为什么还要使用 MVC?
-
使用前请先了解模式。这根本不是它的用途。
-
SO 的重点不是判断别人的代码,而是帮助修复它。这个问题还不错,不值得投票。
-
@jfar:没错。超临界的 cmets 和过度的投票会阻止新用户发布他们的代码。 @48klocs 的评论足以说明问题。
标签: c# asp.net html asp.net-mvc asp.net-mvc-3