.Net Core

.Net Core MVC Bootstrap Modal Using With Jquery

Hello guys,
In this article, I will be talking about using bootstrap modal. I will answer the question of how I can use the bootstrap model with jquery.

1- I created .net mvc core with default template project.
2- I created FakeDataModel;

namespace ModalWithJquery.Models
{
    public class FakeDataModel
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName { get; set; }
    }
}

3- I added CreateModal(GET and POST) method to HomeController

using Microsoft.AspNetCore.Mvc;
using ModalWithJquery.Models;
using System.Diagnostics;

namespace ModalWithJquery.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult CreateModal()
        {
            return PartialView("_ModalView");
        }

        [HttpPost]
        public IActionResult CreateModal(FakeDataModel model)
        {
            // todo db operations etc...

            return Json($"{model.FullName} kaydı eklendi !");
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

4- I edited Views > Home > Index.cshtml

@{
    ViewData["Title"] = "Home Page";
}

<div id="modal-view"></div>

<p>
    <button class="btn btn-primary" type="button" id="btnInsert" data-url="@Url.Action("CreateModal", "Home")">Yeni Kayıt Ekle</button>
</p>

@section Scripts {
    <script>

        $("#btnInsert").click(function (e) {

            var url = $(this).data('url');

            var modal = $("#modal-view");

            $.get(url).done(function (data) {
                modal.html(data);
                modal.find('.modal').modal('show');
            });
        });
    </script>
}

5- I added _ModalView.cshtml in this path: Views > Home > _ModalView.cshtml

_ModalView.cshtml;

<div class="modal fade" id="ModalCrate" tabindex="-1" aria-labelledby="ModalCrateLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <h5 class="modal-title">Yeni Kayıt Oluştur</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>

            <div class="modal-body">

                <div class="mb-3">
                    <label for="FirstName" class="col-form-label" disabled>First Name</label>
                    <input id="FirstName" class="form-control" type="text"/>
                </div>

                <div class="mb-3">
                    <label for="LastName" class="col-form-label" disabled>Last Name</label>
                    <input id="LastName" class="form-control" type="text"/>
                </div>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Kapat</button>
                <button type="button" class="btn btn-primary" id="btnModalSave" data-save="modal-create">Oluştur</button>
            </div>
        </div>
    </div>
</div>

<script>
    $("#btnModalSave").click(function () {
        var requestModel = new Object();
        requestModel.FirstName = $("#FirstName").val();
        requestModel.LastName = $("#LastName").val();
        requestModel.FullName = requestModel.FirstName + " " + requestModel.LastName;

        $.post("/Home/CreateModal", requestModel).done(function (res) {
            $('#modal-view').find('.modal').modal('hide');
            alert(res);
        });
    });
</script>

That’s all.

This is how we show our modal to the screen;
When we press the Create button, we go to the CreateModal method and perform our operations and close the modal.

I wish everyone good work.
semihcelikol.com

Leave a Reply

Your email address will not be published. Required fields are marked *