Save data using modal popup in asp net Core mvc with bootstrap jquery


Recently, I had the opportunity to work on a project where I needed to implement the functionality to edit product details using a modal popup. I faced a few challenges during the process, so I decided to write an article on that topic.

To save data using a modal popup in ASP.NET Core MVC with Bootstrap and jQuery, you can follow these steps: 
Set Up Your Project: Make sure you have a working ASP.NET Core MVC project with the necessary dependencies, including Bootstrap and jQuery. You can include them via CDN or download and reference the files in your project. 
1.Create a Model: Define a model that represents the data you want to save. For example:
public class DemoModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    // Add other properties as needed
}
2.Create a Controller: Generate a controller with actions for displaying the view, handling the form submission, and saving data.
public class TestController : Controller
{
    public IActionResult Index()
    {
        // Retrieve data from the database or initialize as needed
        return View();
    }

    [HttpPost]
    public IActionResult SaveData(DemoModel model)
    {
        // Save data to the database
        // You can use Entity Framework or any other data access method here
        // Example: dbContext.YourModels.Add(model);
        //          dbContext.SaveChanges();
        return RedirectToAction("Index");
    }
}
3.Create a View: Design a view that includes a button to trigger the modal and a form inside the modal for data entry.
@model TestNamespace.DemoModel

<!-- Include necessary Bootstrap and jQuery scripts and stylesheets -->

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
    Open Modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Save Data</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                <!-- Your form for data entry -->
                <form asp-action="SaveData" asp-controller="Your" method="post">
                    <div class="form-group">
                        <label asp-for="Name"></label>
                        <input asp-for="Name" class="form-control" />
                    </div>
                    <!-- Add other form fields as needed -->

                    <button type="submit" class="btn btn-primary">Save</button>
                </form>
            </div>
        </div>
    </div>
</div>
Make sure to include the necessary scripts and stylesheets for Bootstrap and jQuery. Adjust the form fields and layout according to your model and design preferences.  
4.Handle jQuery Modal Events: Include jQuery script to handle modal events, such as hiding the modal after submission.
<script>
    $(document).ready(function () {
        // Triggered when the modal is hidden
        $('#myModal').on('hidden.bs.modal', function () {
            // Reset the form on modal close
            $(this).find('form')[0].reset();
        });
    });
</script>
Ensure that jQuery is properly loaded before this script. Handle Form Submission: In controller, make sure to validate and save the data on form submission.
[HttpPost]
public IActionResult SaveData(YourModel model)
{
    if (ModelState.IsValid)
    {
        // Save data to the database
        // You can use Entity Framework or any other data access method here
        // Example: dbContext.YourModels.Add(model);
        //          dbContext.SaveChanges();
        return RedirectToAction("Index");
    }

    // If the model is not valid, return to the same view
    return View("Index", model);
}
Ensure that you handle validation and any potential errors appropriately. Remember to adjust the code according to your specific requirements, including the model properties, form fields, and data access methods. 

How to Save employee data using modal popup in asp net Core mvc with bootstrap jquery

To save employee data using a modal popup in ASP.NET Core MVC with Bootstrap and jQuery, you can follow these steps: 
Create a Model: Define a model that represents the structure of your employee data. For example:
// EmployeeModel.cs
public class EmployeeModel
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    // Add other properties as needed
}
Create a Controller: Generate a controller with actions for displaying the view and handling the form submission.
// EmployeeController.cs
public class EmployeeController : Controller
{
    public IActionResult Index()
    {
        // Retrieve employee data from the database or initialize as needed
        return View();
    }

    [HttpPost]
    public IActionResult SaveEmployee(EmployeeModel model)
    {
        // Save employee data to the database
        // You can use Entity Framework or any other data access method here
        // Example: dbContext.EmployeeModels.Add(model);
        //          dbContext.SaveChanges();
        return RedirectToAction("Index");
    }
}
Create a View: Design a view that includes a button to trigger the modal and a form inside the modal for data entry.
<!-- Index.cshtml -->
@model EmployeeModel

<!-- Include necessary Bootstrap and jQuery scripts and stylesheets -->

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
    Add Employee
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Add Employee</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                <!-- Your form for employee data entry -->
                <form asp-action="SaveEmployee" asp-controller="Employee" method="post">
                    <div class="form-group">
                        <label asp-for="FirstName">First Name</label>
                        <input asp-for="FirstName" class="form-control" />
                    </div>
                    <div class="form-group">
                        <label asp-for="LastName">Last Name</label>
                        <input asp-for="LastName" class="form-control" />
                    </div>
                    <!-- Add other form fields as needed -->

                    <button type="submit" class="btn btn-primary">Save Employee</button>
                </form>
            </div>
        </div>
    </div>
</div>
Make sure to include the necessary scripts and stylesheets for Bootstrap and jQuery. Adjust the form fields and layout according to your model and design preferences. Handle jQuery Modal Events: Include jQuery script to handle modal events, such as hiding the modal after submission.
<script>
    $(document).ready(function () {
        // Triggered when the modal is hidden
        $('#myModal').on('hidden.bs.modal', function () {
            // Reset the form on modal close
            $(this).find('form')[0].reset();
        });
    });
</script>
Ensure that jQuery is properly loaded before this script. 
Using TempData for Post-Redirect-Get Pattern: If you're using the Post-Redirect-Get pattern, you might want to use TempData to pass a success message to the redirected action. In the SaveEmployee action, after saving the employee data, you can set a success message in TempData:
[HttpPost]
public IActionResult SaveEmployee(EmployeeModel model)
{
    // Save employee data to the database
    // Example: dbContext.EmployeeModels.Add(model);
    //          dbContext.SaveChanges();

    TempData["SuccessMessage"] = "Employee added successfully";

    return RedirectToAction("Index");
}
Then, in the Index action or view, you can check for the presence of TempData["SuccessMessage"] and display it if available. This is a basic example to get you started with saving employee data using a modal popup in an ASP.NET Core MVC application with Bootstrap and jQuery. Adapt the code according to your specific requirements and consider additional features such as validation, error handling, and security measures.

This is a an example to get you started with the integration of a modal popup in an ASP.NET Core MVC application using Bootstrap and jQuery.