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.
- function GetDetails(id) {
- debugger
- $.ajax({
- url: "/JobSeeker/UpdateEducation/" + id,
- type: "GET",
- data: { "Id": id },
- contentType: "application/json;charset=UTF-8",
- dataType: "json",
- success: function (result) {
- alert(result);
- },
- error: function (response) {
- alert(response.responseText);
- }
- });
- return false;
- }
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.
- public JsonResult UpdateEducation( JSProfileVM vm,EducationDetail e,int?id)
- {
- var d = db.EducationDetails.Where(x => x.Id == id).SingleOrDefault();
-
- EducationDetail ed = new EducationDetail();
- ed.DegreeTitle = d.DegreeTitle;
- ed.FieldOfStudy = d.FieldOfStudy;
- ed.Location = d.Location;
- ed.CompletionYear = d.CompletionYear;
- ed.Institution = d.Institution;
- ed.Id = d.Id;
-
- vm.Education = ed;
-
- var json = JsonConvert.SerializeObject(vm);
- return Json(json, JsonRequestBehavior.AllowGet);
- }
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.