Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

c# - return jsonresult behaves unexpextedly with jquery post -


okay please help, i've been frustrated issue.

i need either set error value or perform redirect when user hits submit button. (its standard login form). need ajax.

i think working, when user clicks submit page displays: {"redirect":"/home/index"}

or {"error":"the user name or password provided incorrect."}

its not redirecting/displaying error how i'm intending to.

some background - form login form, , placed inside modal popup dialog.(jqueryui)

heres jquery:

$("#submit").click(function () {             $.post({                 url: "account/logon",                 datatype: "json",                 success: function (data) {                     if (data.redirect) {                         // data.redirect contains string url redirect                         window.location.href = data.redirect;                     }                     else {                         // data.form contains html replacement form                         $("#error").replacewith(data.error);                     }                 }             });             return false;         }); 

and heres action method:

[httppost]     public jsonresult logon(logonmodel model, string returnurl)     {         if (modelstate.isvalid)         {             if (membershipservice.validateuser(model.username, model.password))             {                 formsservice.signin(model.username, model.rememberme);                 if (url.islocalurl(returnurl))                 {                     return json(new { redirect = returnurl });                 }                 else                 {                     return json(new { redirect = "/home/index" });                 }             }          }         return json(new { error = "the user name or password provided incorrect." });     } 

heres form:

@using (html.beginform("logon", "account")) {      <div>         <fieldset>             <legend>account information</legend>              <div class="editor-label">                 @html.labelfor(m => m.username)             </div>             <div class="editor-field">                 @html.textboxfor(m => m.username)                 @html.validationmessagefor(m => m.username)             </div>              <div class="editor-label">                 @html.labelfor(m => m.password)             </div>             <div class="editor-field">                 @html.passwordfor(m => m.password)                 @html.validationmessagefor(m => m.password)             </div>              <div class="editor-label">                 @html.checkboxfor(m => m.rememberme)                 @html.labelfor(m => m.rememberme)             </div>              <p>                 <input id="submit" type="submit" value="log on" />             </p>         </fieldset>     </div> } 

ouch, whole $.post()-call wrong.

use this:

$("#submit").click(function (e) {e.preventdefault();             $.post( "account/logon",                     $(this.form).serialize(),                     function (data)                      {                       if (data.redirect)                        {                         // data.redirect contains string url redirect                         window.location.href = data.redirect;                       }                       else                        {                         // data.form contains html replacement form                         $("#error").replacewith(data.error);                       }                     },                     'json'             );             return false;         }); 

you've used notation of arguments expected in $.ajax() , in $.post() it's different.


Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -