c# - Get a successful Ajax action to open another popup -


right i'm trying have ajax window open window on completion. basically, first window asks if user wants , second tells them successful. first needs yes or no , second needs ok. how tell ajax actionlink open 1 when done? i'm working in mvc 3 c#.

right actionlink is:

@ajax.actionlink("reset user password", "resetuserpw", "admin",  new { username = model.username },  new ajaxoptions { confirm = "reset password?", httpmethod = "httpget" }) 

this works fine (i know there no onsuccess bit, fact mentioned later). executes logic fine , resets user's password. can't figure out how open window. here's controller action:

    public actionresult resetuserpw(string username)     {         string newexcept;         membershipuser user = membership.getuser(username);         if (user != null)         {             try             {                 string newpassword = membership.generatepassword(8, 2);                 if (user.changepassword(user.getpassword(), newpassword))                 {                     var mailmessage = new usermailer();                     return partialview();                     //return redirecttoaction("users"); //tried return                                                            result, no go                 }                 else                 {                     const string erroexcept = "there error processing request (the password reset has failed). please try again.";                     modelstate.addmodelerror("", erroexcept);                 }             }             catch (exception ex)             {                 newexcept = string.format("there error processing request({0}). please try again.", ex.message);                 modelstate.addmodelerror("", newexcept);             }         }         else         {             newexcept = "there no record of specified user in database.";             modelstate.addmodelerror("", newexcept);         }         return redirecttoaction("users");     } 

it never gets last line of code since executes correctly. there no data tags @ top, though have tried changing httpmethod post 1 , adding post tag. also, aware there no onsuccess or oncompletion in actionlink. tried putting whole lot of different things in there , got no results, trimmed them. , after all, entire question put in onsuccess = area?

i'm not great or familiar jquery , think thats why hard me solve. i've searched exhaustively , written tons of different types of jquery code, not use of them ajax link. controller action accepts username since admin action , need users (said stop inevitable: "you can user's user name user.identity.name" statement). also, tell me if trimming exceptions , modelstate code helpful. in advance.

final solution:

public actionresult resetuserpw(string username) {     string newexcept;     membershipuser user = membership.getuser(username);     if (user != null)     {         try         {             string newpassword = membership.generatepassword(8, 2);             if (user.changepassword(user.getpassword(), newpassword))             {                 var mailmessage = new usermailer();                 mailmessage.adminpwreset(user.username, newpassword, user.email.sendasync();                 return json(null);             }             else             {                 modelstate.addmodelerror("password", "there error processing request (the password reset has failed). please try again");             }         }         catch (exception ex)         {             modelstate.addmodelerror("password", string.format("there error processing request ({0}). please try again.", ex.message));         }      }      else      {         modelstate.addmodelerror("invalid user", "there no record of specified user in database.");      }       if (!modelstate.isvalid)      {         return json(getmodelstateerrors(modelstate));      }       return json(null); }     

there no verbs @ top. getmodelstateerrors defined such:

private ienumerable<modelstateerror> getmodelstateerrors(modelstatedictionary dictionary) {     foreach(var key in dictionary.keys)     {         var error = dictionary[key].errors.firstordefault();         if(error != null)             yield return new modelstateerror(key, error.errormessage);     } } 

and finally, ajax link is:

@ajax.actionlink("reset user password", "resetuserpw", "admin", new { username = model.username },  new ajaxoptions {       confirm = "reset password?",       httpmethod = "httpget",       onsuccess="success"       }) 

whoopsies, forgot add final javascript used. same 1 gram said:

<script type="text/javascript"> function success(data) {     alert('you have reset user\'s password!'); } </script> 

you can put javascript callback in onsuccess run if ajaxlink call returns non-error status, so:

<script src="../../scripts/jquery.unobtrusive-ajax.min.js"></script> <script type="text/javascript">     function success(data) {         alert('your password reset');     } </script>  @ajax.actionlink("reset user password", "resetuserpw", "admin",      new { username = model.username },      new ajaxoptions { confirm = "reset password?", httpmethod = "httppost", onsuccess = "success" }) 

but need way return validation errors. try this:

[httppost]  public actionresult resetuserpw(string username) {     string newexcept;     membershipuser user = membership.getuser(username);     if (user != null) {         try {             string newpassword = membership.generatepassword(8, 2);             if (user.changepassword(user.getpassword(), newpassword)) {                 var mailmessage = new usermailer();             }             else {                 modelstate.addmodelerror("password", "there error processing request (the password reset has failed). please try again.");             }         }         catch (exception ex) {             modelstate.addmodelerror("password", string.format("there error processing request({0}). please try again.", ex.message));         }     }     else {         modelstate.addmodelerror("password", "there no record of specified user in database.");     }      if (!modelstate.isvalid)         return json(getmodelstatestateerrors(modelstate));      return json(null); }  private ienumerable<modelstateerror> getmodelstatestateerrors(modelstatedictionary dictionary) {     foreach (var key in dictionary.keys) {         var error = dictionary[key].errors.firstordefault();         if (error != null)             yield return new modelstateerror(key, error.errormessage);     } } 

with simple dto modelstate:

public class modelstateerror {     public string property { get; set; }     public string error { get; set; }      public modelstateerror(string key, string value) {         this.property = key;         this.error = value;     } } 

if there no validation errors, data parameter in success null, otherwise, contain array of validation errors can present user want.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -