109221793
109221793

Reputation: 16897

ascx file error in MVC

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>

Upvotes: 0

Views: 928

Answers (3)

Mike Scott
Mike Scott

Reputation: 12463

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.

Upvotes: 1

Marko
Marko

Reputation: 72232

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>

    <% } %>

Upvotes: 2

sipsorcery
sipsorcery

Reputation: 30699

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

Upvotes: 1

Related Questions