SAP add data to JSON Model on submit button clickk

I’m learning in SAPUI5.
I have to develop a UI form collecting user personal details and office details.
The following is my controller.js file.
in view, the submit button clicks, the model file named jsonSetting, there is a userData, which need to be updated.
json file accessing is done, but in the file is not updating.

sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/m/MessageToast"
],
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller,MessageToast) {
        "use strict";

        return Controller.extend("project3.controller.View1", {
            onInit: function () {

            },

        onSubmit: function(oEvent) {
            var oView = this.getView();
            var sName = oView.byId("idNameInput").getValue();
            var sEmail = oView.byId("idEmailInput").getValue();
            var sDoB = oView.byId("idDoBDatePicker").getValue();
            var sFullAddress = oView.byId("idFullAddressInput").getValue();
            var sState = oView.byId("idStateInput").getValue();
            var sPINCode = oView.byId("idPINCodeInput").getValue();
            var sOfficeAddress = oView.byId("idOfficeAddressInput").getValue();
            var sOfficeState = oView.byId("idOfficeStateInput").getValue();
            var sOfficePINCode = oView.byId("idOfficePINCodeInput").getValue();

            // Log the values to the console
            console.log("Name: " + sName);
            console.log("Email: " + sEmail);
            console.log("Date of Birth: " + sDoB);
            console.log("Full Address: " + sFullAddress);
            console.log("State: " + sState);
            console.log("PIN Code: " + sPINCode);
            console.log("Office Address: " + sOfficeAddress);
            console.log("Office State: " + sOfficeState);
            console.log("Office PIN Code: " + sOfficePINCode);

            let newData= {
                "name": sName,
                "email": sEmail,
                "dob": sDoB,
                "fullAddress": sFullAddress,
                "state": sState,
                "pinCode": sPINCode,
                "officeAddress": sOfficeAddress,
                "officeState": sOfficeState,
                "officePinCode": sOfficePINCode
            }

                // Get the JSON model
    var oModel = this.getView().getModel("jsonSetting");

    // Get existing data
    var aUserData = oModel.getProperty("/userData");
    console.log(aUserData);

    // Add new data
    aUserData.push(newData);

    // Set updated data
    oModel.setProperty("/userData", aUserData);

    // Update bindings (if necessary)
    oModel.updateBindings();

            // Add more fields as needed
   
            // Display a message with the collected data (for testing purposes)
            var sMessage = "Name: " + sName + "\nEmail: " + sEmail + "\nDate of Birth: " + sDoB;
            MessageToast.show(sMessage);
            

            console.log(`event pressed`);
        }
        });
    });

Leave a Comment