Introduction
This article explains how to make a popup model inASP.net core 3.1. We've seen so many issues with this in the latest Jquery and Bootstrap.
In this article we will see how to render partial view from controller and toggle it on button click .
So let's start.
Step 1
Embed the following css & js into your project:
- jQuery v3.4.1
- Bootstrap v4.3.1


Step 2
Create _Model.cs Partial view in shared folder.
- <!--Modal-->
- <div aria-hidden="true" aria-labelledby="modal-create-edit-user-label" role="dialog" tabindex="-1" id="modal-create-edit-user" class="modal fade">
- <div class="modal-dialog">
- </div>
- </div>
Step 3
Create index.cshtml page where you'll create an Add button for clicking to popup.
- @{
- ViewData["Title"] = "Index";
- }
- <a class="btn btn-primary popup" data-url="User/CreateEdit" data-toggle="modal" data-target="#modal-create-edit-user" id="UserModel">Add New User <i class="fa fa-plus"></i></a>
- <partial name="_Modal" />
- @section Scripts{
- <script src="~/js/user.js" asp-append-version="true"></script>
- }
UserController.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using PopupExample.Models;
- namespace PopupExample.Controllers
- {
- public class UserController : Controller
- {
- public IActionResult Index()
- {
- return View();
- }
- public ActionResult CreateEdit(int? id)
- {
- UserDto model = new UserDto();
- model.IsActive = true;
- if (id.HasValue)
- {
- //Write your get user details code here.
- }
- return PartialView("_CreateEdit", model);
- }
- [ValidateAntiForgeryToken]
- [HttpPost]
- public ActionResult CreateEdit(UserDto model)
- {
- //validate user
- if (!ModelState.IsValid)
- return PartialView("_CreateEdit", model);
- //save user into database
- return RedirectToAction("index");
- }
- }
- }
_CreateEdit.cshtml Partial view which you want to render in popup
- @model UserDto
- <!--Modal Body Start-->
- <div class="modal-content">
- <!--Modal Header Start-->
- <div class="modal-header">
- <h4 class="modal-title">Add User</h4>
- <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
- </div>
- <!--Modal Header End-->
- <form asp-action="CreateEdit" asp-controller="User" method="post" enctype="multipart/form-data">
- @Html.AntiForgeryToken()
- <div class="modal-body form-horizontal">
- <div class="form-group row">
- @Html.HiddenFor(model => model.Id)
- @Html.LabelFor(model => model.Name, new { @class = "col-sm-2 col-form-label" })
- <div class="col-sm-10">
- <input asp-for="@Model.Name" placeholder = "Enter Name" class="form-control" />
- </div>
- </div>
- <div class="form-group row">
- @Html.LabelFor(model => model.Address, new { @class = "col-sm-2 col-form-label" })
- <div class="col-sm-10">
- <textarea asp-for="@Model.Address" class="form-control" placeholder="Please enter your address here" />
- </div>
- </div>
- <div class="form-group row">
- <div class="offset-sm-2 col-sm-10">
- <div class="form-check">
- @Html.CheckBoxFor(model => model.IsActive, new { @class = "form-check-input" })
- @Html.LabelFor(model => model.IsActive, new { @class = "form-check-label" })
- </div>
- </div>
- </div>
- </div>
- <!--Modal Footer Start-->
- <div class="modal-footer">
- <button data-dismiss="modal" id="cancel" class="btn btn-default" type="button">Cancel</button>
- <button class="btn btn-success relative" id="btnSubmit" type="submit">
- <i class="loader"></i>
- Submit
- </button>
- </div>
- <!--Modal Footer End-->
- </form>
- </div>
- <!--Modal Body End-->
User.js renders the partial view in popup model
- (function ($) {
- function Index() {
- var $this = this;
- function initialize() {
- $(".popup").on('click', function (e) {
- modelPopup(this);
- });
- function modelPopup(reff) {
- var url = $(reff).data('url');
- $.get(url).done(function (data) {
- debugger;
- $('#modal-create-edit-user').find(".modal-dialog").html(data);
- $('#modal-create-edit-user > .modal', data).modal("show");
- });
- }
- }
- $this.init = function () {
- initialize();
- };
- }
- $(function () {
- var self = new Index();
- self.init();
- });
- }(jQuery));

Here we render partial view......


Output
Now click on AddUser button and add partial view popup in model.

Mohamed eraqatyPosted Oct 19, 2022, 4:20 PM
Is this worked for Bootstrap 5
Paata PeikrishviliPosted May 9, 2022, 7:29 AM
Hello, I have one question, if an error occurred while editing in method public ActionResult CreateEdit(UserDto model), is it possible to not close the dialog box, report the error and allow re-editing?
mahenthiranPosted Sep 22, 2021, 3:45 PM
Can you share the code i am getting some issue in that code. I need code for my refrence asp.net core mvc.5 no
francesco dangeloPosted Sep 6, 2021, 9:43 AM
Hello, i have a problem with this code when a try, i have this error "InvalidOperationException: The partial view '_Modal' was not found. The following locations were searched: /Views/Home/_Modal.cshtml /Views/Shared/_Modal.cshtml", here am I doing wrong?
Chetan Raj SinghPosted Apr 16, 2021, 8:31 PM
Thanks for sharing valuable code, How to get Id to show data in modal popup?, This is only for add new record.
greenglassPosted Mar 16, 2021, 9:16 AM
Well, but UserDto, PopupExample.Models - where they are?
Paata PeikrishviliPosted Feb 19, 2021, 1:59 PM
After click, Can't Call CreateEdit(int? id)
Nalin JayasuriyaPosted Dec 20, 2020, 2:19 PM
Thanks so much Khushbu, easy to follow. Great article! Some quick feedback. In the JavaScript code, a simplification is possible: This line: $('#modal-create-edit-user > .modal', data).modal("show"); I think can be simplified to: $('#modal-create-edit-user').modal("show"); This is because the 'modal' class is in the same 'modal-create-edit-user' element in your example . The second parameter to set the select 'data' is not needed because the HTML was set earlier line of code with: .html(data); Thanks!
Joyab KhanPosted Nov 2, 2020, 4:11 AM
Very Nice article ):-
john sPosted Oct 7, 2020, 11:38 PM
This is working for me ...nice
kamal majeedPosted Sep 28, 2020, 11:03 AM
It didn't work for me, first the name of the partial is _Modal and not _Model.cs as you mentioned in your article (Create _Model.cs Partial view in shared folder). Second, it doesn't bring the footer .. I don't know much about javascripts but maybe there is something wrong with user.js ?