MVC中的ascx文件错误

时间:2010-10-24 21:54:21

标签: c# asp.net-mvc partial-views

我正在尝试在另一个视图中呈现部分ascx视图。

但是我的ascx文件中有以下错误,经过一些研究后我还在黑暗中!:

  

类型或命名空间定义,或   文件结尾预期文件预期

以下是DinnerForm.ascx中的代码

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<NerdDinner.Models.Dinner>" %>

<%: Html.ValidationSummary("Please Corrent the Errors and Try Again.") %>

        <fieldset>
            <legend>Fields</legend>

<table border="0">
          <tr>
            <td><%: Html.LabelFor(m => m.Title) %></td>
            <td><%: Html.TextBoxFor(m => m.Title) %></td>
            <td><%: Html.ValidationMessageFor(m => m.Title, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.EventDate) %></td>
            <td><%: Html.TextBoxFor(m => m.EventDate) %></td>
            <td><%: Html.ValidationMessageFor(m => m.EventDate, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.Description) %></td>
            <td><%: Html.TextAreaFor(m => m.Description) %></td>
            <td><%: Html.ValidationMessageFor(m => m.Description, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.Address) %></td>
            <td><%: Html.TextBoxFor(m => m.Address) %></td>
            <td><%: Html.ValidationMessageFor(m => m.Address, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.Country) %></td>
            <td><%: Html.DropDownListFor(m => m.Country, ViewData["countries"] as SelectList)%></td>
            <td><%: Html.ValidationMessageFor(m => m.Country, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.ContactPhone) %></td>
            <td><%: Html.TextBoxFor(m => m.ContactPhone) %></td>
            <td><%: Html.ValidationMessageFor(m => m.ContactPhone, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.Latitude) %></td>
            <td><%: Html.TextBoxFor(m => m.Latitude) %></td>
            <td><%: Html.ValidationMessageFor(m => m.Latitude, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.Longitude) %></td>
            <td><%: Html.TextBoxFor(m => m.Longitude) %></td>
            <td><%: Html.ValidationMessageFor(m => m.Longitude, "*") %></td>
          </tr>
          <tr>
            <td><input type ="submit" value="Save" /></td>
          </tr>
        </table>
        </fieldset>

    <% } %>

以下是我在名为create.aspx

的文件中使用它的示例
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Models.Dinner>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Host a Dinner
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Host a Dinner</h2>
    <% Html.RenderPartial("DinnerForm"); %>
    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>

3 个答案:

答案 0 :(得分:2)

请注意<% } %>文件底部的.ascx如何?这是<% using (Html.BeginForm()) {%>的结束括号,你似乎错过了。

添加

<% using (Html.BeginForm()) {%> 

正好位于

之下
<%: Html.ValidationSummary("Please Corrent the Errors and Try Again.") %>

结果表格

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<NerdDinner.Models.Dinner>" %>

<%: Html.ValidationSummary("Please Corrent the Errors and Try Again.") %>
<% using (Html.BeginForm()) {%>
        <fieldset>
            <legend>Fields</legend>

<table border="0">
          <tr>
            <td><%: Html.LabelFor(m => m.Title) %></td>
            <td><%: Html.TextBoxFor(m => m.Title) %></td>
            <td><%: Html.ValidationMessageFor(m => m.Title, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.EventDate) %></td>
            <td><%: Html.TextBoxFor(m => m.EventDate) %></td>
            <td><%: Html.ValidationMessageFor(m => m.EventDate, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.Description) %></td>
            <td><%: Html.TextAreaFor(m => m.Description) %></td>
            <td><%: Html.ValidationMessageFor(m => m.Description, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.Address) %></td>
            <td><%: Html.TextBoxFor(m => m.Address) %></td>
            <td><%: Html.ValidationMessageFor(m => m.Address, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.Country) %></td>
            <td><%: Html.DropDownListFor(m => m.Country, ViewData["countries"] as SelectList)%></td>
            <td><%: Html.ValidationMessageFor(m => m.Country, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.ContactPhone) %></td>
            <td><%: Html.TextBoxFor(m => m.ContactPhone) %></td>
            <td><%: Html.ValidationMessageFor(m => m.ContactPhone, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.Latitude) %></td>
            <td><%: Html.TextBoxFor(m => m.Latitude) %></td>
            <td><%: Html.ValidationMessageFor(m => m.Latitude, "*") %></td>
          </tr>
          <tr>
            <td><%: Html.LabelFor(m => m.Longitude) %></td>
            <td><%: Html.TextBoxFor(m => m.Longitude) %></td>
            <td><%: Html.ValidationMessageFor(m => m.Longitude, "*") %></td>
          </tr>
          <tr>
            <td><input type ="submit" value="Save" /></td>
          </tr>
        </table>
        </fieldset>

    <% } %>

答案 1 :(得分:1)

删除&lt;%}%&gt;从DinnerForm.ascx的底部开始。

答案 2 :(得分:1)

你的ascx文件末尾有一个未配对的<% } %>

表格遗失。看起来你省略了:

<% using( Html.BeginForm() ) { %>

从验证摘要之前的顶部开始。