locked
Add and Edit Records in json file in mvc5 RRS feed

  • Question

  • User1839056048 posted

    Hi

    I have an application in mvc5

    Following is ths code for Adding and editing data in database

            [HttpPost]
           
            public ActionResult Save(Customer customer)
            {
                
    
                if (customer.Id == 0)
                    _context.customers.Add(customer);
    else { var customerInDb = _context.customers.Single(c => c.Id == customer.Id); customerInDb.Name = customer.Name; customerInDb.Birthdate = customer.Birthdate; } _context.SaveChanges(); return RedirectToAction("Index", "Customers"); }

    My requirement is I want to add an d edit data in customer.json file,

    fields are Id,name,Birthdate and id is auto generated

    for adding

    if (customer.Id == 0)

    _context.customers.Add(customer);//adding to database it is working fine

    id=customer.id,name=customer.name

    birthdate=customer.birthdate

    I wants to add above data in to customer.json

    for editing

    else {

    var customerInDb = _context.customers.Single(c => c.Id == customer.Id);

    customerInDb.Name = customer.Name;

    customerInDb.Birthdate = customer.Birthdate;

    //edt above data into customer.json file

    //here i wants to write the update code for json file

    }

    _context.SaveChanges();

    how it is possible

    Regards

    Baiju

    Thursday, May 21, 2020 9:37 AM

All replies

  • User475983607 posted

    I don't understand the design logic since it looks like you are storing the data in a table but creating a JSON file is very very simple and well documented.   The Json.NET reference documentation illustrates how to serialize an object to a file.

    https://www.newtonsoft.com/json/help/html/SerializeWithJsonSerializerToFile.htm

    Thursday, May 21, 2020 10:51 AM
  • User1686398519 posted

    Hi,  klbaiju

    According to your needs, I made a simple example. This example involves serialization and deserialization of Json string.

    public ActionResult SaveJson()
            {
                string path = Path.Combine(Server.MapPath("~/Json/"), "customer.json");
                string havejson = System.IO.File.ReadAllText(path);
                List<Test> haveObj = new List<Test>();
                if (!String.IsNullOrEmpty(havejson)) {
                    JsonConvert.DeserializeObject<List<Test>>(havejson).ForEach(h => {
                        haveObj.Add(h);
                    });
                }
                //Suppose this is the data of add
                Test test = new Test
                {
                    Id = 1,
                    Name = "Test1"
                };
                haveObj.Add(test);
                string testjson = JsonConvert.SerializeObject(haveObj, Newtonsoft.Json.Formatting.Indented);
                System.IO.File.WriteAllText(path, testjson);
                return View();
            }
            public ActionResult EditJson(int Id)
            {
                string path = Path.Combine(Server.MapPath("~/Json/"), "customer.json");
                string editjson = System.IO.File.ReadAllText(path);
                List <Test> jsonObj = JsonConvert.DeserializeObject<List<Test>>(editjson);
                jsonObj.ForEach(j =>
                {
                    if (j.Id == Id)
                    {
                        j.Name = "Test2";
                    }
                });
                string haveeditjson = JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
                System.IO.File.WriteAllText(path, haveeditjson);
                return View();
            }

    Here is the result.

     
    Best Regards,

    YihuiSun

    Tuesday, May 26, 2020 8:57 AM