asp.net mvc - How to add success message after HttpPost submits database changes -


i have httppost method add database

public actionresult submitdata(myviewmodel model) {     if (modelstate.isvalid)     {         var result = submitdata(command);          if (response.success)            {             return redirecttoaction("myhttpgetmethod");         }     }      return view("myhttpgetmethod", model); }  public actionresult myhttpgetmethod(int id) {     mymodel model = getdata(id);      return view(model); } 

after httppost called , database changes made successfully, redirect httpget action current data after changes. display success message on view. can't use viewbag because of redirect , can't use tempdata, because it's not recommended here.

tempdata valid solution specific use case. if not prefer that, can pass querystring value indicate status of transaction httppost action action.

so update action have paramter

public actionresult myhttpgetmethod(int id,string m="") {     mymodel model = getdata(id);     if(!string.isnullorempty(m) && m=="s")     {       // code show success message here.       viewbag.msg="saved successfully";     }     return view(model); } 

and in view, use viewbag item show message (with whatever styles want)

<p>@viewbag.msg</p> 

and in httppost action,

return redirecttoaction("myhttpgetmethod", new { id =model.id, m="s"} ); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -