#1 2017-05-12 06:30:58

Joker
Member
Registered: 2015-07-06
Posts: 15

Is this possible with an MVC server?

Hi All,
I have an MVC server currently working well, however I want to be able to call services that don't return a page.

For example I have a page where an admin can reset a users password just by clicking a button. The page doesn't need to change (and I don't want it to change), however I may want to display a "dialog" to say that the password reset has been successful.

So my questions are:
1. Can I do this within the MVC framework?
2. If so, how?
3. If not, what are my other options?

Thanks in advance.

Joker.

Offline

#2 2017-05-12 09:17:03

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Is this possible with an MVC server?

The MVC feature is mostly server-side.
So for such case, the default way is to return a page.

I see two options (but there may be other ways):

1. Add a REST AJAX call.

In the Mustache template, add some JavaScript to display the dialog box.
Then define an interface-based service, in addition to the MVC server, to handle the classical AJAX request.

2. Redirect to a HTML sub-frame.

In the Mustache template, add some JavaScript to display the dialog box as a HTML sub-frame.
Then fill the dialog frame from a dedicated page on the MVC server.

Perhaps the option 2 is more easy to setup.

Offline

#3 2017-05-16 01:31:47

Joker
Member
Registered: 2015-07-06
Posts: 15

Re: Is this possible with an MVC server?

Thanks ab. I might try option 2 and see how I go.

Offline

#4 2017-05-16 04:48:55

Joker
Member
Registered: 2015-07-06
Posts: 15

Re: Is this possible with an MVC server?

Actually I found a better way to do this using the "/json" tag at the end of the URL. This means I can call the function without creating a new server and still use the authentication/cookie data as I would for normal page calls. Here is a quick example of the js function I call :

function resetpassword(client_id, user_name){
        $.post("/admin/resetpassword/json",
        {
          ClientID: client_id,
          UserName: user_name
        },
        function(data,status){
          if (data.ResetPasswordResult.Success)
            {
              document.getElementById("dialog_title").innerHTML = "Reset Password";
              document.getElementById("dialog_content").innerHTML = "Password reset successful";
              $("#ModalDialog").modal();
            }
          else
            {
              document.getElementById("dialog_title").innerHTML = "Reset Password FAILED!";
              document.getElementById("dialog_content").innerHTML = data.ResetPasswordResult.ErrMsg;
              $("#ModalDialog").modal();
            }
        })
        .fail(function() {
          document.getElementById("dialog_title").innerHTML = "Reset Password FAILED!";
          document.getElementById("dialog_content").innerHTML = "Unable to access server";
          $("#ModalDialog").modal();
        })
        ;
}

Offline

#5 2017-05-16 08:03:17

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Is this possible with an MVC server?

Yes, this is a nice trick!
Reusing the very same cookie content for authentication and safety is definitively a plus.
And, on the server side, you don't need to define additional methods or interfaces services.
You got a brilliant idea!

The /json suffix was meant for debugging/testing, but in fact it could be used for actual REST service implementation.
smile

Thanks for sharing.

Offline

#6 2017-09-12 14:54:31

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Is this possible with an MVC server?

Hello,

So on the server-side function "/admin/resetpassword", instead of returning a HTML page, you return a JSON string, right?
Thanks.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#7 2017-09-12 15:10:39

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Is this possible with an MVC server?

By adding /json

Offline

Board footer

Powered by FluxBB