This is my controller for adding data in backend through UI , but i am getting error oModel.create is not a function
onOpenDialog: function () {
if (!this._oDialog) {
this._oDialog = new Dialog({
title: "Add New Product",
content: [
new VBox({
items: [
new Input("dialogInputID", {
placeholder: "Enter Product ID"
}),
new Input("dialogInputName", {
placeholder: "Enter Product Name"
}),
new Input("dialogInputDescription", {
placeholder: "Enter Description"
}),
new Input("dialogInputReleaseDate", {
placeholder: "Enter Release Date"
})
]
})
],
beginButton: new Button({
text: "Submit",
press: this.onAddProduct.bind(this)
}),
endButton: new Button({
text: "Cancel",
press: function () {
this._oDialog.close();
}.bind(this)
})
});
this.getView().addDependent(this._oDialog);
}
this._oDialog.open();
},
onAddProduct: function () {
// Retrieve data from dialog inputs
var oID = sap.ui.getCore().byId("dialogInputID").getValue();
var oName = sap.ui.getCore().byId("dialogInputName").getValue();
var oDescription = sap.ui.getCore().byId("dialogInputDescription").getValue();
var oReleaseDate = sap.ui.getCore().byId("dialogInputReleaseDate").getValue();
// Validate inputs
if (!oID || !oName || !oDescription || !oReleaseDate) {
sap.m.MessageToast.show("Please fill in all fields.");
return;
}
// Create new product object
var oNewProduct = {
ID: parseInt(oID), // Ensure ID is an integer
Name: oName,
Description: oDescription,
ReleaseDate: oReleaseDate // Format date as required
};
// Get the OData model
var oModel = this.getView().getModel("ODataM1");
if (oModel) {
// Create a new entry in the backend
oModel.create("/Products", oNewProduct, {
success: function () {
console.log("Product added successfully!"); // Debugging statement
sap.m.MessageToast.show("Product added successfully!");
// Close the dialog
if (this._oDialog) {
this._oDialog.close(); // Close the dialog
} else {
console.error("Dialog instance is not available.");
}
// Refresh the table to display new data
var oTable = this.getView().byId("productTable");
if (oTable) {
oTable.getBinding("items").refresh(); // Refresh the table binding
}
}.bind(this),
error: function (oError) {
console.error("Error adding product:", oError);
sap.m.MessageToast.show("Error adding product.");
}
});
} else {
console.error("The OData model is not instantiated or is not available.");
}
},
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.