Search code examples
asp.net-mvclayoutrazorsubmithtml.beginform

Submit button not working inside @section mvc


I got this problem and i can't find a solution. I put a submit button inside a @section so I can put it inside a div in the layout page. But the problem is that the submit button doesn't work when inside the section.

Here is the view, the section en the submit button are almost at the bottom of the page

@{
var vd = new MvcVerkoop.Models.VerkoopsData();
var data = vd.KoperData; }

@using (Html.BeginForm()){
@Html.ValidationSummary(true)

<fieldset>
    <legend>Kies de nodige velden</legend>


    <table class="velden">

        @foreach (string var in data)
        {

            <tr>
                <td>
                    @Html.CheckBox(var)
                    <label for="Naam">@var</label>
                </td>
                <td>
                    @Html.CheckBox("Verplicht" + @var)
                    <label for="Verlpicht">Verplicht veld</label>
                </td>
            </tr>

        }


        <tr>
            <td>
                <div id="input1" class="clonedInput">

                    @Html.TextBox("extra1")
                </div>

            </td>

            <td>
                <div id="inputCheck1" class="clonedInputCheck">
                    @Html.CheckBox("checkVerplicht1")

                    <label for="checkVerplicht1" id="labelVerplicht1">Verplicht veld</label>
                </div>

            </td>
        </tr>

    </table>

    <div>
        <input type="button" id="btnAdd" value="+" />
        <input type="button" id="btnDel" value="-" />
    </div>

    @section Bottom{

        <input id="btnSubmit" type="submit" value="Doorgaan" />
    }

</fieldset>}

When the submit button is outside the section is works fine.

EDIT: layout code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
        <div class="top-menu">

        </div>
</header>
<div id="body">
    <div class="centre-box">
        <div class="centre-banner">
        </div>

        @RenderSection("featured", required: false)
    <section class="content-wrapper main-content clear-fix">
            @RenderBody()
        </section>
        <div class="centre-footer">
            @RenderSection("Bottom", false)
        </div>
    </div>

</div>
<footer>
        <div class="content-wrapper">
            <div class="float-left">
                <p>© @DateTime.Now.Year Code Express</p>
            </div>
        </div>
    </footer>
</body>
</html>

Can somebody help me? Thanks!


Solution

  • I can only assume that when the button is inside the section that it is being rendered outside of the form.

    I assume you want to put the button inside the section so you can share that button across many forms? If so you'll have to use JavaScript to hook a submit on the click event of the button, because the button is no longer part of a form.

    You'll also then have to hook up an event for when someone hits enter too to submit the form.

    I would just create a distinct button for each form you want to submit. A lot less hassle.