服务器块格式不正确

时间:2019-02-26 13:09:51

标签: asp.net-mvc model-view-controller

我正在尝试在MVC中添加aspx视图而不是Razor View。

<%@ Html.DisplayNameFor(model => model.Name) %>行出现错误

解析器错误消息:服务器块的格式不正确。 <---在浏览器中

  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="WebApplication1.Views.EmployeesPart25MVC.List" %> <%@ Import Namespace="WebApplication1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">    <head runat="server">
          <title></title>    </head>    <body>
          <form id="form1" runat="server">
             <div>
                <h2>
                   Gomathi
                </h2>
                <table class="table">
                   <tr>
                      <th>
                         <%@ Html.DisplayNameFor(model => model.Name) %>
                      </th>
                      <th>
                         <%@ Html.DisplayNameFor(model => model.Gender) %>
                      </th>
                      <th>
                         <%@ Html.DisplayNameFor(model => model.City) %>
                      </th>
                      <th>
                         <%@ Html.DisplayNameFor(model => model.DepartmentsPart25MVC.Name) %>
                      </th>
                      <th></th>
                   </tr>
                   <%@foreach (var item in Model) 
                      { %>
                   <tr>
                      <td>
                         <%@ Html.DisplayFor(modelItem => item.Name) %>
                      </td>
                      <td>
                         <%@ Html.DisplayFor(modelItem => item.Gender) %>
                      </td>
                      <td>
                         <%@ Html.DisplayFor(modelItem => item.City) %>
                      </td>
                      <td>
                         <%@ Html.DisplayFor(modelItem => item.DepartmentsPart25MVC.Name) %>
                      </td>
                      <td>
                         <%@ Html.ActionLink("Edit", "Edit", new { id=item.EmployeeID }) |
                            Html.ActionLink("Details", "Details", new { id=item.EmployeeID }) |
                            Html.ActionLink("Delete", "Delete", new { id=item.EmployeeID })  %>
                      </td>
                   </tr>
                   <%@ } %>
                </table>
             </div>
          </form>    </body>

    </html>

1 个答案:

答案 0 :(得分:0)

在aspx引擎中<%@ %>仅用于页面指令,不能在其他地方使用。

对于所有HTML输出,通常应使用<%= %>

<%= Html.DisplayNameFor(model => model.Name) %>

对于代码块,仅<% %>

<% foreach (var item in Model) 
   { %>