tags:

views:

149

answers:

2

I have a standard Edit form view within MVC, and I need to place a user control, in this case Create inside the BeginForm, like below:

When the issue is that the Create ascx when the form is submitted does not fire it's action in the controller, what am I doing wrong?

<% using (Html.BeginForm())
   {%>
<fieldset>
    <legend>Tax</legend>
    <p>
        <label for="Name">
            Name:</label>
        <%= Html.TextBox("Name", Model.Tax.Name) %>
        <%= Html.ValidationMessage("Name", "*") %>
    </p>
    <p>
        <%  Html.RenderAction("Create", "Mileage"); %>
    </p>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
<% } %>

Here is the Create.ascx

<% using (Html.BeginForm())
   {%>
<fieldset>
    <p>
        <label for="Distance">
            Distance:</label>
        <%= Html.TextBox("Distance", Model.Mileage.Distance)%>
        <%= Html.ValidationMessage("Distance", "*") %><span class="field-validation-error"
            id="field-validation-error-distance">*</span>
    </p>
</fieldset>
<% } %>
+3  A: 

You have nested forms in your resulting HTML. This will not work as expected. Remove the form from the inner view. The inner view will then be incomplete, so if you were using it as a stand-alone, you should make it shared, and create another view, which will just open the form, render the inner view, and close the form.

As a margin note: you are not using the default binder. You can if you want to, it will work even with nested objects (Html.TextBox("Mileage.Distance"), for example).

Palantir
+1  A: 

Nested form are not supported in HTML. See here: The FORM element

Every form must be enclosed within a FORM element. There can be several forms in a single document, but the FORM element can't be nested.

Either remove the form from your partial view and let the container view provide it, or remove the form from the view and add it to the partial thus making it independent of the parent.

Developer Art