繁体   English   中英

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

[英]ASP.NET MVC 3 Html helper not recognized

由于某种原因,我的 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>

谁知道怎么修它?

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

 <html>
      <body>
          <div>
              @GetSiteVisits()
         </div>    
      </body>
  </html>
  @helper GetSiteVisits()
  {
        @Html.Raw("hello");
  }

您需要导入命名空间:

@using System.Data.SqlClient
@using System.Data
@using Mine.MyHtmlHelper.MyNameSpace // Here!!

function GetSiteVisits() 需要在您的 controller 中,而不是视图中。 该方法生成的数据(从数据库读取)然后进入视图 model,该视图被传递给视图。 在粗略的伪代码中,它应该是这样的:

Model

public class IPVisits
{
    public string IPAddress { get; set; }
    public int NumVisits { get; set; }
}

查看 Model

public class SiteVisitsViewModel
{
    public List<IPVists> Visits { get; set; }
}

Controller

// SiteVisits method only
public ActionResult SiteVisits(string id)
{
    // Get the data for the UserHostAddress
    list = Repository.GetTheDataInListForm(id);
    return View(list);
}

看法

foreach( var d in Model.Visits)
{
    <tr>
        <td>@d.IPAddress</td>
        <td>@d.NumVisits</td>
    </tr>
}

确保它在您的 web.config 文件中(可能在根目录以及 ~/Views 文件夹中):

<system.web>
    <compilation debug = "true" targetFramework = "4.0">
        <assemblies>
            <add assembly = "System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add assembly = "System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add assembly = "System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add assembly = "System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            <add assembly = "System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        </assemblies>
    </compilation>
</system.web>

重要的一行真的只是System.Web.Mvc, ...

这是 asp.net MVC,MVC 表示 Model(业务逻辑),视图(表示层,或客户端看到的)和 controller,它与请求一起运行。 为什么在 View 中使用 Model。 在 Model class 中使用 sql 进行操作是正确的,在 Z594C103F2C6E04C3D8AB09 中调用它并发送到 View by a View. 没有理由以这种方式使用 asp.net MVC。

在您的 Views 目录中应该有一个 Web.config。 您只需要将命名空间添加到您的助手

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM