ASP.NET MVC 3 Html助手无法识别

mas*_*der 0 html c# asp.net asp.net-mvc asp.net-mvc-3

由于某种原因,我的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>
Run Code Online (Sandbox Code Playgroud)

谁知道怎么修它?

Man*_*kar 5

编写Html助手的方法是在助手中返回字符串.

 <html>
      <body>
          <div>
              @GetSiteVisits()
         </div>    
      </body>
  </html>
  @helper GetSiteVisits()
  {
        @Html.Raw("hello");
  }
Run Code Online (Sandbox Code Playgroud)