How can i return bootstap 5 model as view from controller?
My controller:
public IActionResult Details(int id)
{
..........
return View();
}
I want to pass details from above controller to bootstrap model. How can achieve this?
Sachin SinghPosted Jan 2, 2023, 2:33 PM
The simple answer is NO.
You can return a partial view and on ajax success append the returned partial view to your Modal Popup's body. Then simply open the Popup.
Vishal YelvePosted Jan 2, 2023, 1:18 PM
In Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
NorthwindEntities entities = new NorthwindEntities();
return View(from customer in entities.Customers.Take(10)
select customer);
}
[HttpPost]
public ActionResult Details(string customerId)
{
NorthwindEntities entities = new NorthwindEntities();
return PartialView("Details", entities.Customers.Find(customerId));
}
}
In View
@model IEnumerable
@{
Layout = null;
}
Customers
@foreach (Customer customer in Model)
{
}
Customer Details Form
×
$(function () {
$("#CustomerGrid .details").click(function () {
var customerId = $(this).closest("tr").find("td").eq(0).html();
$.ajax({
type: "POST",
url: "/Home/Details",
data: '{customerId: "' + customerId + '" }',
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (response) {
$("#partialModal").find(".modal-body").html(response);
$("#partialModal").modal('show');
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
});
});
In Partial View
@model Partial_View_MVC.Customer
@Html.DisplayFor(model => model.Address)
@Html.DisplayFor(model => model.City),
@Html.DisplayFor(model => model.PostalCode)
@Html.DisplayFor(model => model.Country)
Pasang TamangPosted Dec 30, 2022, 11:42 AM
Below link will gives you more explanation about using partial view as Boootstrap modal
https://www.aspsnippets.com/Articles/Render-Partial-View-inside-Bootstrap-Modal-Popup-in-ASPNet-MVC.aspx
Pasang TamangPosted Dec 30, 2022, 11:38 AM
Hi,
You should be able to load partial view as a Bootstrap modal. Please check code example from below link.
https://gist.github.com/shaneprice/bc69d6e318b063493948
Regards,
Pasang