Hi @jewel
add record using partial view and bootstrap modal, But validation not work in asp.net core
To solve this issue, just move the JavaScript scripts from partial view to the main view.
In the main view (index). the code like this: remember to change controller's name to yours in the url.
@model VenderModel
@{
ViewData["Title"] = "Index2";
}
<div class="col-10 text-end">
<button onclick="AddVender(0)" class="btn btn-primary btn-sm">Add New Vender</button>
</div>
<div class="modal fade" id="myModal1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<a href="#" class="close" data-bs-dismiss="modal">×</a>
<h3 class="modal-title text-bg-success" id="heading">Insert record</h3>
</div>
<div class="modal-body" id="myModalBodyDiv1">
</div>
</div>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script>
function AddVender() {
var url = "/Home/PartialLoad";
$("#myModalBodyDiv1").load(url, function (data) {
$("#myModal1").modal("show");
})
}
function addata() {
var objdata =
{
vender_Name: $('#VenderName').val()
};
$.validator.unobtrusive.parse($("#myform"));
if ($("#myform").valid()) {
$.ajax({
type: 'POST',
url: '/Home/InserData',
data: objdata,
dataType: 'json',
dataType: 'json',
success: function () {
alert("Save")
},
error: function () {
alert("Not Saved")
}
});
}
}
</script>
}
And the partial view like this:
@model Test.Models.VenderModel
<form id="myform">
<div class="form-group row p-1">
<div class="col-4">
<label asp-for="vender_Name">Vender Name</label>
</div>
<div class="col-4 ">
<input asp-for="vender_Name" placeholder="VenderName" id="VenderName" autocomplete="off" />
<span asp-validation-for="vender_Name" class="text-danger"></span>
</div>
</div>
<div class="modal-footer">
<button type="button" onclick="addata()">Save</button>
</div>
</form>
Then, the result as below:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion