asp.net mvc - MVC ModelState.Clear isn't updating ModelState -
in mvc program, once user has submitted edit form, values object saved in model server-side, previous values show in view.
i know has mvc's validation process, first checks modelstate before server side values. solution i've read across forums clear modelstate. problem is, modelstate.clear isn't working me.
help please.
model
public class { [hiddeninput(displayvalue=true)] public int helpid { get; set; } [required(errormessage = "please enter proper url")] public string url { get; set; } [required(errormessage = "please enter content description:")] [datatype(datatype.multilinetext)] public string helpcontent { get; set; } /*? 2 properites nullable*/ public datetime? createdatetime { get; set; } public datetime? modifieddatetime { get; set; } }
controller
/*create admin controller*/ public class admincontroller : controller { //declare interface object private ihelprepository repository; /*pass db interface controller*/ public admincontroller(ihelprepository repo) { repository = repo; } /*default admin screen. displays table obs*/ public viewresult index() { return view(); } /*returns add view form*/ public actionresult addform() { return partialview(); } /*will handle post add screen after user has submitted add information*/ [httppost] [validateinput(false)] //this allows admin place html in field public actionresult addform(help help) { if (modelstate.isvalid) //if fields validated { //set edit date help.createdatetime = datetime.now; repository.savehelp(help); return (null); //return "null" div control given main view } else //there wrong. send view { return partialview(help); } } /*returns edit view form, searches object edit id if no id provided, 0 default*/ public actionresult editform(int helpid = 0) { help = repository.help.firstordefault(q => q.helpid == helpid); help.helpcontent = system.web.httputility.htmldecode(help.helpcontent); return partialview(help); } /*will handle post edit screen after user has submitted edit information*/ [httppost] [validateinput(false)] //this allows admin place html in field public actionresult editform(help help) { if (modelstate.isvalid) //if fields validated { //set edit date help.modifieddatetime = datetime.now; repository.savehelp(help); modelstate.clear(); return (null); //return "null" div control given main view } else //there wrong. send view { return partialview(help); } } /*delete action method, searches id*/ [httppost] public actionresult delete(int helpid) { helpdel = repository.help.firstordefault(p => p.helpid == helpid); if (helpdel != null) //if object found, delete { repository.deletehelp(helpdel); } //in cases return index return redirecttoaction("index"); } /*used telerik table rebind grid*/ [gridaction] public actionresult ajaxbinding() { return view(new gridmodel(repository.help)); } }//end admin controller class`
partial view (gets loaded div) `
@using (html.beginform(null, null, formmethod.post, new { id = "editx" })) { @html.validationsummary(true) <fieldset> <legend>edit entry</legend> @html.hiddenfor(model => model.helpid) <div class="editor-label"> @html.labelfor(model => model.url) </div> <div class="editor-field"> @html.editorfor(model => model.url) @html.validationmessagefor(model => model.url) </div> <div class="editor-label"> @html.labelfor(model => model.helpcontent, "help content") </div> <div class="editor-field"> @{ html.telerik().editorfor(content => content.helpcontent) .name("helpcontent") .filebrowser(settings => settings .browse("browse", "imagebrowser") .thumbnail("thumbnail", "imagebrowser") .upload("upload", "imagebrowser") .deletefile("deletefile", "imagebrowser") .deletedirectory("deletedirectory", "imagebrowser") .createdirectory("createdirectory", "imagebrowser") ) .render(); } @html.validationmessagefor(model => model.helpcontent) </div> <div class="editor-label"> @html.labelfor(model => model.createdatetime, "create date") </div> <div class="editor-field"> @html.editorfor(model => model.createdatetime) @html.validationmessagefor(model => model.createdatetime) </div> <div class="editor-label"> @html.labelfor(model => model.modifieddatetime, "modified date") </div> <div class="editor-field"> @html.editorfor(model => model.modifieddatetime) @html.validationmessagefor(model => model.modifieddatetime) </div> <p> <input id="btnedit" type="submit" value="save" /> <button id="btncancel">cancel</button> </p> </fieldset> }
after trolling through hundred links, found solution problem. modelstate.clear clears objects values in controller, whatever reason still displaying old values in view. maybe because load/unload edit form div tag? don't ask. don't know. solution works me this:
$.ajax({ url: "somecontroller/someaction, cache: false, // key make sure jquery not cache request success: function( data ) { alert( data ); } });
i had set "cache" setting "false".
thanks @minus4 solution, bruh.
Comments
Post a Comment