Asp.Net MVC from in modal bootstrap

ajaxasp.net-mvcbootstrap

First, some of the technologies in play: Asp.Net Mvc 5, Bootstrap 3, JQuery.

On my view I want to create a bootstrap model dialog, with a simple form in it.
When the user clicks submit, there are two outcomes.

1) Invalid input, should display error message from the server

2) OK input, redirect/refresh the same page, so the dialog is closed and pagedata is updated.

Since the modal dialog is shown using javascript, I guess I have to post to the server using JQuery ajax post? That seems to be the best way to keep the modal dialog visible, if there is an error?

I'm also unsure about where to place the post action. In the same controller or perhaps in a new web api controller (since this I'm doing a ajax post).

Best Answer

Yes, you should use an ajax call to send the request and receive the response, using the response data to determine whether to display an error message or close the modal dialog.

As for where to place the action, while you certainly can create a separate WebAPI controller, and eventually you may decide to do so for architecture reasons, you can easily accomplish what you want by placing the action in your existing controller and designing it to return the appropriate JSON response.