How can i trigger the submit
button from partial view without using Html.BeginForm
tag in Partial View. Reason being when there are two form tags, that is in main view and partial view, the validation doesn't work.
The important thing is that I want the validation to work.
My Partial View code:
<div id="AddMe">
<div class="row">
@Html.LabelFor(model => model.FirstName)
@Html.TextBoxFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName) // this should work
</div>
</div>
<div>
<input name="submit" type="submit" id="submit" value="Save" /> // how do i trigger this from partial view
</div>
My Main View code:
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
{
<div class="modal" id="modalId" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
</div>
<div class="modal-body" id="modalbodyId">
@Html.Partial("PartilViewName")
</div>
<div class="modal-header">
<button type="submit">
</div>
</div>
</div>
</div>
}
For Validation purpose, I have added the following lines of code in Main View.
$("#modalId").click(function () {
var form = $("#modalbodyId").closest("form");
form.removeData('validator');
form.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse(form);
$.ajax({
url: "/ControllerName/ActionName",
type: "POST",
data: $("#modalbodyId").serialize(),
cache: false
});
});
Also just for the records all the models and other Annotations are in place.
Note: Do i need to add the
unobtrusive/ajax
code inside Partial View.