2010-10-24 50 views
0

我試圖在另一個視圖中呈現部分ascx視圖。MVC中的ascx文件錯誤

我有但下面的錯誤在我的ASCX文件,經過一番研究,我仍然在黑暗中!:

類型或命名空間的定義,或 檔案結尾expectedend-OF-文件預計

這裏是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> 

回答

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> 

    <% } %> 
+0

謝謝你,我應該看到那個!一旦時間限制到達,我會將其標記爲正確答案:) – 109221793 2010-10-24 22:05:22

+0

@TaraWalsh - 將代碼移動到ascx文件時,忘記複製它並不難。我昨天剛剛完成了NerdDinner的書,所以仍然有新的記憶:) – Marko 2010-10-24 22:13:31

1

從DinnerForm.ascx的底部刪除<%}%>。

+0

這將糾正編譯錯誤,但ascx顯然是一種形式,所以他需要打開頂部的表單。 – 2010-10-24 22:07:54

1

你有一個未配對的正下方在你的ascx文件的末尾。

表格丟失。看起來像你省略:

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

從頂部,就在驗證總結之前。