asp.net mvc - Return Error or message of a view on another view in mvc -
there login modal on _layout.cshtml page , user can login @ time or @ page. when modal load ,we appending 'login' view modal $("#divmodal").load("/login/index");
while submitting form , works fine. wondering if there way display error or custom message "login failed" on dialogbox on same page except using jquery ajax.
1) tried return view() redirecting page login/index page. 2) can't use partial view have pass login model every other models.
besides using jquery ajax , there other way achieve this.
you can use jsonresult
if error occurs. write custom action filter on server catches exception , transforms them json response:
public class myerrorhandlerattribute : filterattribute, iexceptionfilter { public void onexception(exceptioncontext filtercontext) { filtercontext.exceptionhandled = true; filtercontext.result = new jsonresult { data = new { success = false, error = filtercontext.exception.tostring() }, jsonrequestbehavior = jsonrequestbehavior.allowget }; } }
then in view can add call in ajax call:
error: function(xhr) { try { // try/catch recommended error handler // occur in many events , there might not // json response server var json = $.parsejson(xhr.responsetext); alert(json.errormessage); } catch(e) { alert('something bad happened'); } }
Comments
Post a Comment