user3070072
user3070072

Reputation: 620

How to add a pop up dialog?

I am writing to know, how can I create a pop-up dialog box, whenever "api_login is null". I would also like to know, how can I put url link in the pop-up box as well.

 public api_login Getapi_login(string id)
    {
        api_login api_login = db.api_login.Find(id);
        if (api_login == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            //Response.Write("<script>alert('Your alert box')</script>");
        }

        return api_login;
    }

Many thanks

Upvotes: 0

Views: 336

Answers (3)

BlackjacketMack
BlackjacketMack

Reputation: 5692

We use an attribute on the Controller method called 'CustomExceptionHandler' (for instance). In it we take the description we want and pass that to the Response.StatusDescription property. Once the response is returned to our calling page, we have a little js that reads the status description and outputs whereever (for instance, in an alert). It looks like this:

Sample Action

[CustomExceptionHandler]
        public ActionResult Save(SomeViewModel viewModel)
        {
//do stuff

}

CustomExceptionHandler class

public class CustomExceptionHandler : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        handleException(filterContext);
    }

    private void handleException(ExceptionContext filterContext)
    {
        filterContext.HttpContext.Response.StatusCode = 500;
        filterContext.HttpContext.Response.StatusDescription = HttpUtility.UrlEncode(filterContext.Exception.Message);

        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.End();
    }
}

Javascript (for example with an ajax call)

function onFormFailure(context) {
    alert(context.statusText);
}

The thing we like about it is that the messaging of the exception is separated from the display. So if we wanted to display the message in an alert, or modal, or inline we could change it independently.

Upvotes: 0

Mister Epic
Mister Epic

Reputation: 16743

You need to generate the alert from your Javascript code on your client. The following leverages jQuery's AJAX functions:

$.ajax( {
  url: "/YourUrl",
  data: { id = yourIdVariable } 
 })
.done(function(jqXHR) {
     alert( "success" );
})
.fail(function(jqXHR) {
     alert( "Error with status code" + jqXHR.status );
})
.always(function(jqXHR) {
    alert( "complete" );
});

ref: http://api.jquery.com/jquery.ajax/

Upvotes: 0

Dumisani
Dumisani

Reputation: 3048

You can use JQuery popups to achieve that

Upvotes: 1

Related Questions