Diego Diego -3 years ago 73
HTML Question

MVC form submit executes action from another Partial View

I am working with

MVC 5
. I have a
View
filled with two
Partial view
. One is de
Header
and the other is the
Body
.

I have this in my header.schtml



using (Html.BeginForm("Login", "Home"))
{
<div class="DvBotonLogM" >
<input type="submit" value="Logi IN" title="Log" class="btn2 btn-primary AlinearBoton" />
</div>
}





And I have this in my

Body.cshtml




<form action="@Url.Action("Create", "Users")" id="formCreate" enctype="multipart/form-data" method="post">
@Html.AntiForgeryToken()

<div class="dvUploadCaptcha">
<div class="dvRegistroAct">
<input type="submit" value="Grabar" class="btn btn-primary" />
</div>
</div>
</form>





When I
sumbit
formCreate
I do it via
Jquery
.



$('formCreate').submit(function (event) {
event.preventDefault();
if ($(this).valid()) {
var formdata = new FormData($(this).get(0));
$.ajax({
url: this.action,
type: this.method,
data: formdata,
processData: false,
contentType: false,
beforeSend: function () {
if (!validarTerms())
return false;
},
success: function (result) {
processResponse(result);
},
complete: function () {

}
});
}
return false;
});





The problem I have is when I
submit
Header View, it calls Body
submit
.

How can I prevent doing this, and just call
Sumbit
action from Header View?

Answer Source

add your form in a container

<div class="myContainer">

 <form action="@Url.Action("Create", "Users")" id="formCreate" enctype="multipart/form-data" method="post">
                @Html.AntiForgeryToken()

                <div class="dvUploadCaptcha">
                    <div class="dvRegistroAct">
                        <input type="submit" value="Grabar" class="btn btn-primary" />
                    </div>
                </div>
 </form>

</div>

and in the script:

 $('.myContainer #formCreate').submit(function (event) {

            });

Do the same for the other form and perhaps make the script code into a function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download