How to return a message after sending

I have the following code which is not working as expected. I want to get a fetch from a controller and with an alert display the value returned by the controller.

 $('#change').dialog({
            autoOpen: false,
            width: 380,
            buttons: {
                "Close": function() {
                    $(this).dialog("close");
                },
                "Accept": function() {
                    var test = $("#ChangePasswordForm").submit();
                    alert(test);
                }
            }
        });

      

In my controller, I want to return a string

  [AcceptVerbs(HttpVerbs.Post)]
    public string ChangePassword(string Name)
    {
        var msg = "Cool!";
if (name != null)


return msg;
                }

      

How can i do this?

+1


a source to share


4 answers


The controller should return a type generated from ActionResult.

If you want to display a simple confirmation message, you can add it to the ViewData package like this:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult ChangePassword(string name)
    {
        if (!string.IsNullOrEmpty(name))
        {
            ViewData["msg"] = "Cool";
        }
        return View();
    }

      



Then, in your opinion, check for the existence of the value and display it if it is:

<% if(ViewData["msg"] != null) { %>
    <script type="text/javascript">alert('<%= ViewData["msg"].ToString() %>')</script>
<%} %>

      

+1


a source


  [AcceptVerbs(HttpVerbs.Post)]
  public ActionResult ChangePassword(string Name)
  {
        var msg = "Cool!";
        if (name != null)
        {       
            return Content(msg, "text/plain");
        }
        else
        {
            return Content("Error...", "text/plain");
        }
   }

      



+1


a source


First of all, I am assuming you are using an ajax form for this. I will also assume that you have something to enter text. All you have to do is set the UpdateTargetId to point to the id of the item you want to update with the text

<%using (Ajax.Form("ChangePasswordForm", new AjaxOptions { UpdateTargetId = "result" })) %>

      

...

[HttpPost]
public ContentResult ChangePassword(string s)
{
      var msg = "Cool!";
      if ( s != null ? return Content(msg, "text/plain") : return Content("An error has occured", "text/plain") );
}

      

+1


a source


Don't submit the form as it will postback and cause the dialog to be deleted.

Instead, do an AJAX post to Action Controller and return a JsonResult containing data.

Grab the success callback from the Ajax request and raise an alert passing the data from the Json object.

You probably won't use the boot mask after the button is clicked to let the user know that something is happening.

0


a source







All Articles