Development Tools and Key Technologies: Visual Studio ASP.NET MVC
Author: Liu Jianhong
Writing time: Saturday, 13 April 2019
Programmer's Daily Operations - Modifying Data
The operation of data modification is something programmers can encounter every day in managing data. Modifications and additions are very similar, their purpose is to modify data in the database, and the new data is from scratch. The modification is based on the original to modify, through the acquisition of the primary key ID to determine which data to modify, and in the modification operation, data backfilling.
In the controller, we need to use a pass parameter, which is the data passed from the page modification. Next we need to do five steps in the controller. 1. Determine whether the data already exists in the database. 3 Get the data to be modified. 3. Extract data 4 according to the primary key ID, execute modification 5 and save the modified dataTo the database. Here is the code for the modification operation:
The code is as follows:
public ActionResult UpdateAcademe(SYS_Academe sysAcademe) //sysAcademe is the passed parameter { ReturnJson returnJson = new ReturnJson(); try { //Check for conflicts with others int otherCount = (from tbAcademe in myModel.SYS_Academe where tbAcademe.AcademeID != sysAcademe.AcademeID && ( tbAcademe.AcademeName == sysAcademe.AcademeName.Trim() || tbAcademe.AcademeCode == sysAcademe.AcademeCode.Trim()) select tbAcademe).Count(); if (otherCount ==0) {//Get the data to be modified SYS_Academe dbAcademe = (from tbAcademe in myModel.SYS_Academe //Data extraction based on primary key ID where tbAcademe.AcademeID == sysAcademe.AcademeID select tbAcademe).Single(); //Assignment of entities dbAcademe.AcademeName = sysAcademe.AcademeName; dbAcademe.AcademeCode =sysAcademe.AcademeCode; //Execution modification myModel.Entry(dbAcademe).State = EntityState.Modified; //Save and modify if (myModel.SaveChanges()>0) { returnJson.State = true; returnJson.Text = "Successful revision!"; } else { returnJson.State = false; returnJson.Text = "Modification failed!"; } } else { returnJson.State = false; returnJson.Text = "And other repetitions!"; } } catch (Exception e) { Console.WriteLine(e); returnJson.State = false; returnJson.Text = "Data exception!"; } return Json(returnJson, JsonRequestBehavior.AllowGet); }
There is another section in the view, mainly two methods. Open the Modified Mode Box. 2 Save Modified Modal Box
When opening the modal box, in order to ensure the quality of the data, we need to clear the modal box, so reset the form. Then the data is backfilled, and the post asynchronous submission is also used when backfilling. There are five steps to save and modify the modal box. 1 // Get page data 2 judges that the data of the input box is not empty 3 and asynchronously submits 4 closed modal box 5 to refresh the table.
Actually, it's no different from the new operation. The code is as follows
//Open Modified Mode Box function openUpdate(UacademeId) { //Reset Form $('#formUpdateAcademe input[type="reset"]').click(); //Backfill data $.post("/SysemMangement/Collegeinfor/SelectAcademeById", { academeId: UacademeId }, function (data) { loadDatatoForm("formUpdateAcademe", data);//Fill the form form form according to the json object }, "json"); //Pop-up modal box $("#modalUpdateAcademe").modal('show'); } //Save and modify function savaUpdate() { //Get page data var AcademeID = $("#UAcademeID").val(); var AcademeName = $("#UAcademeName").val(); var AcademeCode = $("#UAcademeCode").val(); //judge if (AcademeID != '' && AcademeID != undefined &&!isNaN(AcademeID) && AcademeName != '' &&AcademeName != undefined && AcademeCode != '' &&AcademeCode != undefined) { //Asynchronous submission of data $.post("/SysemMangement/Collegeinfor/UpdateAcademe", { AcademeID: AcademeID, AcademeName:AcademeName, AcademeCode:AcademeCode }, function (returnJson) { if (returnJson.State == true) { //Close the modal box $("#modalUpdateAcademe").modal('hide'); //Refresh table tabAcademe =layuiTable.reload('tabAcademe'); } layer.alert(returnJson.Text); }, "json"); } else { //Tips layer.alert('Please complete the form.', { title: 'Tips', icon: 0 }); } }
Note: The code in the view is all jQuery code.