umair mohsin

umair mohsin

  • 1.3k
  • 351
  • 56.1k

query about json.

Mar 21 2021 10:42 AM
Getting database records using json and viewmodel but no idea how to bind this with view(inside textboxes) in mvc.
 
In my application I am using json(used it to avoid page refresh) to fetch data from database.data is fetched and showing in an alert box but I have no idea how to display a single record value in an alert(this is only for test purpose,finally I will bind this record to a textbox in mvc). I am posting a chunk of code I am using in my application
 
Jquery Ajax
 
An html anchor tag(somewhere in a page) is hitting this method.table is there to show certain details of a database table from which record is to be fecthed.
  1. function GetDetails(id) {  
  2. debugger  
  3. $.ajax({  
  4. url: "/JobSeeker/UpdateEducation/" + id,  
  5. type: "GET",  
  6. data: { "Id": id },  
  7. contentType: "application/json;charset=UTF-8",  
  8. dataType: "json",  
  9. success: function (result) {  
  10. alert(result);  
  11. },  
  12. error: function (response) {  
  13. alert(response.responseText);  
  14. }  
  15. });  
  16. return false;  
  17. }  
Controller Method
 
Ajax hits this method in a controller,this method then fetching record processing this and return result in a json format. Please don’t reply me regarding this method, its perfectly showing me my database record in json format.
  1. public JsonResult UpdateEducation( JSProfileVM vm,EducationDetail e,int?id)  
  2. {  
  3. var d = db.EducationDetails.Where(x => x.Id == id).SingleOrDefault();  
  4. //vm.GetEducation();  
  5. EducationDetail ed = new EducationDetail();  
  6. ed.DegreeTitle = d.DegreeTitle;  
  7. ed.FieldOfStudy = d.FieldOfStudy;  
  8. ed.Location = d.Location;  
  9. ed.CompletionYear = d.CompletionYear;  
  10. ed.Institution = d.Institution;  
  11. ed.Id = d.Id;  
  12. ////Profile();  
  13. vm.Education = ed;  
  14. //ViewBag.Msg = "Profile Updated Successfully";  
  15. var json = JsonConvert.SerializeObject(vm);  
  16. return Json(json, JsonRequestBehavior.AllowGet);  
  17. }
The only issue is when goes to result in jquery ajax alert showing me whole data is json format and I have no idea how to show even a single record and bind this to textboxes. Please help me out.

Answers (4)