asp.net mvc 3 - Check, Nothing is changed in Model values MVC3 -
i've model following:
public class page { private readonly indiantime _g = new indiantime(); public page() { createdon = _g.datetime; properties = "published"; tags = "page"; relativeurl = string.empty; } public string path { { return (parentpage != null) ? parentpage.heading + " >> " + heading : heading; } } [key] public int id { get; set; } [stringlength(200), required, datatype(datatype.text)] public string title { get; set; } [stringlength(200), required, datatype(datatype.text)] public string heading { get; set; } [maxlength, required, datatype(datatype.html)] public string content { get; set; } [display(name = "reference code"), scaffoldcolumn(false)] public string referencecode { get; set; } [required] [remote("checkduplicate", "page", errormessage = "url has taken", additionalfields = "initialurl")] public string url { get; set; } [display(name = "created on"), scaffoldcolumn(false)] public datetime createdon { get; set; } //parent page object (self reference: parentid = > id) [display(name = "parent page")] public int? parentid { get; set; } [displayformat(nulldisplaytext = "root")] public virtual page parentpage { get; set; } public virtual icollection<page> childpages { get; set; } }
during update, there check model values "changed" or "nothing has changed" after post
method?
say example: (see commented lines)
[httppost, validateantiforgerytoken] public actionresult edit(webpage webpage) { try { if (modelstate.isvalid) { // if (webpage object values unchanged, submitted // { // not insert values table // } // else // { // inert table // } _db.entry(webpage).state = entitystate.modified; _db.savechanges(); return redirecttoaction("index"); } } catch (dataexception) { modelstate.addmodelerror("", errorcode.msg.contactsystemadmin.tostring()); } return view(webpage); }
you can this
[httppost, validateantiforgerytoken] public actionresult edit(webpage webpage) { try { if (modelstate.isvalid) { var dbentry=_db.getentrybyid(webpage.id); //check here if properties in webpage , column values in dbentry //same decide whether insert in table or not _db.entry(webpage).state = entitystate.modified; _db.savechanges(); return redirecttoaction("index"); } } catch (dataexception) { modelstate.addmodelerror("", errorcode.msg.contactsystemadmin.tostring()); } return view(webpage); }
or not sure if works
try using $(form).serialize()
@ client side
Comments
Post a Comment