How can I make a completely different Modal appear in the View, depending on the response from the Controller?

I have a code on my Asp.Net Core APP which I want to handle exclusively through Modals and with responses from the Controller, which change depending on the values that are sent from the View.

Right now I have the following code, which, what it does is change the message in a div on the Modal, for the response it receives from the controller, with the button that calls said Modal.

General part of the view:

@model AP.ViewModels.UK1

<div class="container">
    <div class="card level-3">
        <h3>Ac</h3>
        <hr />
        <div class="row">
            <div class="col-md-4">
                <form asp-action="Create">
                    <div class="form-group">
                        <input asp-for="UK2" class="form-control" />
                        <span asp-validation-for="UK2" class="text-danger"></span>
                    </div>

                    <div class="form-group">
                        <input asp-for="UK3" class="form-control" />
                        <span asp-validation-for="UK3" class="text-danger"></span>
                    </div>


                    <div class="form-group">
                        <input type="submit" value="UK5" class="btn btn-primary" /> |
                        <!-- Button to Open the Modal -->
                        <button id="btnOpenModal" type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
                            CALL CONTROLLER / MODAL BUTTON
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

Modal Code on the View:

<!-- The Modal -->
<div class="modal" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">

            <!-- Modal Header -->
            <div class="modal-header">
                <h4 class="modal-title">CONTROLLER RESPONSE:</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- Modal body -->
            <div class="modal-body" id="modalcontent">
            </div>


            <!-- Modal footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">CANCEL</button>
                <button type="button" class="btn btn-primary" data-dismiss="modal">OK1</button>
            </div>
        </div>
    </div>
</div>

Script which calls Modal and sent the data to the Controller:

<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script>
    $(function () {
        $("#btnOpenModal").click(function () {
            var uk = {};
            uk.UK2 = $("#UK2").val();
            uk.UK3 = $("#UK3").val();
            $.ajax({
                type: "POST",
                url: "/UK1/GetViewContent",
                data: uk,
                beforeSend: function (request) {
                    request.setRequestHeader(
                        "RequestVerificationToken",
                        $("[name='__RequestVerificationToken']").val());
                },
                success: function (data) {

                    $('#modalcontent').html(data);
                },
                error: function (response) {
                    $("#myModal").modal('toggle')

                }
            });
        });

        $("#myModal").on("click", ".btn-default", function () {
              
                alert("Cancel button click");
            });
        
        $("#myModal").on("click", ".btn-danger", function () {
            // code
            alert("Delete button click");

            $('#myModal').modal('hide') 
        });
    });
</script>

Controller Code:

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult GetViewContent(UK1 uk)
{

if (uk.UK2 == uk.UK3)
{
                
return Ok("A-CASE 1");
}

if (uk.UK2 >= uk.UK3)
{
                
return Ok("B-CASE 2");
}

if (uk.UK2 <= uk.UK3)
{
                
return Ok("C-CASE 3");
}
            
            
if (uk.UK2 == null)
{
                
return Ok("D-CASE 4");
}
            
if (uk.UK3 == null)
{
                
return Ok("E-CASE 5");
}
            
 }       
         
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(UK1 ukk)
{
            
return View("Home1");
}
        
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateDos(UK1 ukk)
{
            
return View("Home2");
}

Now this is what I want to achieve with the code:
I would like my code to have 5 possible Modals, one for each possible response from the Controller, and that each one of these Modals had a different message, as well as different buttons, and my question is, how can I do it? Which are my options?

The first thing that comes to my mind is to have HTML code for 5 different Modals in view, and depending on which the Controller’s response is, the code calls a different one of the Modals, the problem is that I don’t know how to do that, since I don’t know how to read the controller Response as a ‘variable’ in the script code, or how I should put "Ifs" that depend on the response there in the Script, but I understand that this should go in this part of the code:

                success: function (data) {

                    $('#modalcontent').html(data);
                },
                error: function (response) {
                    $("#myModal").modal('toggle')

                }

In any case, what I would like for my 5 Modals, is something similar to this:

1)If the answer that is received from the Controller is "A-CASE 1", the Modal should get an "A" message on the div, and just the Cancel button should appear at the botton of the Modal.

2)If the answer that is received from the Controller is "B-CASE 2", the Modal should get an "B" message on the div, and both the Ok and Cancel button should appear at the botton of the Modal, the Ok Button should call me the Controller’s Create method.

3)If the answer that is received from the Controller is "C-CASE 3", the Modal should get an "C" message on the div, and both the Ok and Cancel button should appear at the botton of the Modal, the Ok Button should call me the Controller’s CreateDos method.

4)If the answer that is received from the Controller is "D-CASE 4", the Modal should get an "D" message on the div, and just the Cancel button should appear at the botton of the Modal.

5)If the answer that is received from the Controller is "E-CASE 5", the Modal should get an "E" message on the div, and just the Cancel button should appear at the botton of the Modal.

Anyway, thanks for reading everything and thanks in advance, all this is simply because I try to learn how to make the Modal Script do different things, and consider different cases, depending on what is the response that is sent from the Controller, since I understand that the complexity of the problem arises that the variables of the Script environment exist at different times than the variables of the View, and I don’t know to what extent it is possible to treat the ‘response’ sent by the controller as a Variable, but I would like to learn how to do it if possible, and I want to understand all this.

2 thoughts on “How can I make a completely different Modal appear in the View, depending on the response from the Controller?”

  1. You really make it seem so easy with your presentation but I find this matter to be actually something that I think I would never
    understand. It seems too complex and extremely broad for
    me. I am looking forward for your next post, I will try to
    get the hang of it!

    Reply

Leave a Comment