Reloading the ASP.NET MVC bootstrap modal using Ajax for a fresh look

I'm struggling with my bootstrap modal. Whenever I click the submit button, the page refreshes and the modal disappears. How can I keep the modal open after clicking the submit button to display either a success or error message? I am new to MVC and haven't been able to solve this issue.

Here's my modal:

<div class="modal fade" id="signin_modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class=" modal-header">
                Create An Account
            </div>

            <div class="modal-body">

                @using (Html.BeginForm("Login", "Home", FormMethod.Post))
                {
                    <table>


                        <tr><td>@Html.Label("username", null, new { style = " font-size:12px; font-weight:normal; font-family:consolas; " })</td></tr>
                        <tr><td>@Html.TextBox("name", null, new { style = " width:200px; height:30px; " })</td></tr>


                        <tr> </tr>

                        <tr><td>@Html.Label("password", null, new { style = " font-size:12px; font-weight:normal; font-family:consolas; " })</td></tr>
                        <tr><td>@Html.TextBox("password", null, new { style = " width:200px; height:30px " })</td></tr>

                    </table>
                }

            </div>

            <div class="modal-footer">
                <button type="submit" class="btn btn-success">Sign in</button>
                @*<button type="reset" class="btn btn-warning">Reset</button>*@
                <button type="reset" class="btn btn-default">Reset</button>
            </div>

        </div>
    </div>
</div> 

Answer №1

Although this may be an older question, here is my approach to handling it.

If you are utilizing strongly typed view models, one way to handle this is by adding a property called IsModalShown like so:

public class Foo
{
    public bool IsModalShown { get; set; }
}

Include this as a hidden field in your view:

@Html.HiddenFor(m => m.IsModalShown)

When the modal needs to be displayed, update the hidden value to true and post back to the controller action:

$('#signin_modal').on('show.bs.modal', function (e) {
    $('#IsModalShown').val(true);
})

Keep in mind that the code snippet above can vary depending on the Bootstrap version you're using, but official documentation can provide guidance.

Finally, include the following script in your view to automatically show the modal:

$(function(){
    @if(Model.IsModalShown)
    {
        $('#signin_modal').modal('show');
    }
});

You can also use properties in the model to populate other fields within the modal.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering Issues with Ajax Request in the Google Play App

I am facing an issue with my Phonegap app. It functions perfectly in debug mode and when installed as an apk file from Phonegap. However, when I upload it to Google Play Store and then download it from there, my ajax requests stop working. I have added the ...

Tips for converting file byte code to file form data

From one folder I am retrieving a file and uploading it to another server. However, when I extract the file from the first folder, I receive a byte code representation of that file. The API for uploading the file to the second folder requires FormData as a ...

Having trouble with running 'npm init -y' in Windows? Here's a workaround for the VS

An error occurred when trying to initialize npm with the command "npm init -y". The name "WEB DEV" was deemed invalid. For a detailed log of this issue, please refer to: C:\Users\User\AppData\Roaming\npm-cache_logs\2020-12-05 ...

Enhance Your Website with Interactive Tooltips Using Twitter Bootstrap

In Twitter bootstrap, the default trigger for tooltips is hover. If I want to make the tooltip display on focus instead, I can add data-trigger="focus". But how do I make it so the tooltip displays both on hover and on focus? ...

Node.js: The REST client delivers the response even before it has been officially returned

I've been experimenting with the node-rest-client REST client in Node.js. However, I'm facing an issue where when I run the code snippet below, it returns a value of null. Oddly enough, the response is printed to the console after that. Is there ...

directive still running despite attempting to stop the service

In my modal window, there is a directive that utilizes a service to make HTTP requests at regular intervals using $interval. However, even after closing the modal window, the service continues to run and make requests every 30 seconds. Is there a way to ...

A guide on achieving a dynamic color transition in Highcharts using data values

I am currently working on plotting a graph using high charts and I would like to change the color based on the flag values. I attempted this, however, only the points are changing based on the flag values and the line is not being colored accordingly. Be ...

Issues with the functionality of the asynchronous socket.io JavaScript callback are being experienced

I am facing an issue with my JavaScript code that involves querying data from a database using Node.js and Socket.io. Currently, I have implemented setTimeout() functions to make it work, but I want to switch to using callbacks for better reliability. Howe ...

Concealing divs without values in ASP.NET MVC

I am working on an AJAX call to fetch data from the back-end and populate divs with it. Below is my code for the AJAX call: $(document).ready(function() { question_block(); }); function question_block() { $.ajax({ url: '@Url.Action(" ...

Is it possible to manipulate the attribute of an object using Object.defineProperty when a value is passed as a function parameter?

As I delve into understanding reactivity in Vue, the concept of how reactivity is achieved when passing a value as a function parameter perplexes me. //here is the actual code snippet var obj = {a: 'aa'} function reactive(obj, key, value) { ...

sending a POST request with fetch

When it comes to making AJAX requests, I used to rely on jQuery in the past. However, with the rise of React, there is no longer a need to include the entire jQuery library for this purpose. Instead, it is recommended to use JavaScript's built-in fetc ...

"Seeking assistance with implementing a Toggle Button using AJAX in an ASP.NET environment

My goal is to dynamically update the page each time the checkbox is checked or unchecked using a Toggle Button from AJAX. However, I have been facing difficulties in achieving this functionality. Although it works perfectly with a button and an image butto ...

I encountered an infinite loop issue with Material UI tabs when loading my component within the navigation bar (AppBar > TabPanel)

I am currently working on an application for a school project, and I'm incorporating Material UI into it. Following the official documentation provided on their website. This is the link I have been referring to: Code. My goal is to load a new comp ...

What could be the reason for my component not getting the asynchronous data?

Currently, I am delving into the intricacies of React and have been following a tutorial that covers creating components, passing props, setting state, and querying an API using useEffect(). Feeling confident in my understanding up to this point, I decided ...

How to include a Partial View or Child Page into a Master Layout using jQuery/Ajax in ASP.NET MVC

Currently, I am in the process of working on a CRM Admin panel that utilizes a left-hand side panel for navigation to various pages. Within this panel, there are buttons such as Home, About, and Contact. To streamline the layout, I have implemented a mast ...

How can I automatically update a div's value when adding or editing data in an SQL table?

I am currently working with MVC 5 .net and Entity Framework. My goal is to create a client dashboard that displays the latest data from a SQL database. Can anyone advise me on how to call an ajax method or refresh a specific table/div when any values are ...

Utilizing Laravel's eloquent capabilities to perform a dynamic join operation via

Hello! I'm looking for information on how to use Laravel Eloquent join in an AJAX success function. Can anyone help? Here is the User Model: <?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation& ...

Experiencing difficulties establishing a connection between the React client and Node.js server using SocketIO

I am currently getting familiar with socketIO and have successfully set up a basic nodejs server. However, I am facing an issue where my nextJS app is unable to connect to the server as expected. Despite no errors being displayed, the messages that should ...

I keep encountering the error message "ReferenceError: window is not defined" in Next.js whenever I refresh the page with Agora imported. Can someone explain why this is happening?

Whenever I refresh my Next.js page with Agora SDK imported, I keep encountering the error "ReferenceError: window is not defined". It seems like the issue is related to the Agora import. I attempted to use next/dynamic for non-SSR imports but ended up with ...

The attention remains fixed at the top of the page

I have implemented an update panel along with pagination links using a repeater control at the bottom of my page. However, I am encountering an issue where clicking on the pagination links does not bring the page to the top. I attempted to use the followin ...