views:

31

answers:

3

Hi guys,

I'm trying to render a partial ascx view within another view.

I have the following error however in my ascx file, and after some research I'm still in the dark!:

Type or namespace definition, or end-of-file expectedend-of-file expected

Here is the code in 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>

    <% } %>

And here is an example of how I am using it in a file called 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>
+1  A: 

Remove the <% } %> from the bottom of your DinnerForm.ascx.

sipwiz
That would correct the compilation error, but the ascx is clearly a form, so he needs to open the form at the top.
Mike Scott
+2  A: 

Notice how you have <% } %> at the bottom of the .ascx file? That is a closing bracket for <% using (Html.BeginForm()) {%> which you seem to have missed out.

Add

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

to just below the

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

Resulting form

<%@ 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>

    <% } %>
Marko
@Marko, thank you, I should have seen that one! I will mark this as the correct answer once the time limit is up :)
TaraWalsh
@TaraWalsh - it's not very hard to forget to copy that when you move the code to an ascx file. I just finished the NerdDinner book yesterday so still have a fresh memory of it :)
Marko
+1  A: 

You've got an unpaired <% } %> at the end of your ascx file.

The form is missing. Looks like you omitted:

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

from the top, just before the validation summary.

Mike Scott