In the previous article, we leave a challenge: how to pass the JSON data submitted by the client browser through ajax to an existing object (that is, to assign the JSON data to the corresponding attribute of the object ). In this way, Entity Framework will automatically discover which attribute values have changed. When saving, it will only update the changed attribute values to the corresponding database fields.
Note that the attributes in the JSON data submitted here are dynamic and do not necessarily contain all attributes of the object. For example, the submitted attribute is {"title ": "Hello World"}. The next submission is {"title": "Hello World", "Description": "This is a test. "}. The first scenario corresponds to the application scenario where the blog background provides a function to quickly Modify the title. A text box for modifying the title is displayed in the pop-up layer.
Here, we will emphasize how the Entity Framework implements on-demand updates. First, an object before modification is given, and then the attribute value of the current object is modified. EF can track the changes and update the changes to the database when saving the changes.
In the face of this problem, you may have come up with this method. Directly deserialize JSON data into an object (the controller of ASP. net mvc can automatically complete this deserialization), and then copy the attribute value of this object to the object tracked by EF. However, you do not know which attribute values of the deserialized object come from JSON data. Copying all objects is not feasible. As mentioned above, attributes in JSON data are dynamic and the deserialized object is not a complete object. If you forcibly copy all data, data in attributes not included in json data will be lost when the data is updated.
The solution is to obtain attributes in JSON data through JSON. net, and then modify the attribute values of the object tracked by EF through reflection.
Below we useCodeThe example shows the solution.
JS Code for submitting JSON data through Ajax in the client browser:
Function Update_post (){
VaR Post = {};
Post. Title = $. Trim ($ ( " # Txttitle " ). Val ());
Post. Description = $. Trim ($ ( " # Txtdescription " ). Val ());
VaR Jsonstr = JSON. stringify (post );
VaR Entry = {};
Entry. postid = Entryid;
Entry. jsondata = Jsonstr;
$. Ajax ({
URL: ' /Admin/ajaxupdatepost. aspx ' ,
Data: JSON. stringify (entry ),
Success: update_post_callback
});
}
Actual submitted JSON data:
{"Postid":3560,"Jsondata":"{\ "Title \": \ "Hello world \", \ "Description \": \ "test \"}"}
Code in ASP. net mvc controller:
[Httppost]
PublicJsonresult ajaxupdatepost (IntPostid,StringJsondata)
{
ReturnJSON (servicefactory. blogentryservice. Update (postid, jsondata ));
}
The code is very simple. It is just a call to the service layer, and then the server layer calls the business logic layer.
Code in the business logic layer:
Public Class Blogentrymanager
{
// Object Data Update
Public Bool Update ( Int Postid, String Jsondata)
{
Blogpost originalpost = Getblogpost (postid );
Using (Blogdbcontext Context = New Blogdbcontext ())
{
Context. blogposts. Attach (originalpost );
Jsontoobject (originalpost, jsondata );
Context. savechanges ();
}
Return True ;
}
// Copy JSON data to object
Private Void Jsontoobject (blogpost post, String Jsondata)
{
Jobject jo = Jobject. parse (jsondata );
VaR posttype = Post. GetType ();
Foreach (VAR proper In Jo. properties ())
{
Posttype. getproperty (proper. Name). setvalue (post ,( String ) Proper. value, Null );
}
}
// Get object before modification
Public Blogpost getblogpost ( Int Entryid)
{
Using (Blogdbcontext Context = New Blogdbcontext ())
{
VaR result = From P In Context. blogposts
Where P. ID = Entryid && P. isexist
Select P;
Return Result. firstordefault ();
}
}
}
The Code has passed the test and verified that it can meet our current needs.
The modernization of the blog Park has taken another step forward!