cancel
Showing results for 
Search instead for 
Did you mean: 

Transfer Values Between two SAP UI5 Views

ianfmccallum
Explorer
0 Kudos

Hello:

I used the example at the following link to try to set, and then read, a model in the Core.

The code in the First (sending) Controller is:

onAddPressed: function(oEvent) {
	var oCustomerID = this.getView().byId("CustomerID").getText();
	var modelParameters = {};
	modelParameters.CustomerID = oCustomerID;
	this.customer.setData(modelParameters);
			
	sap.ui.getCore().setModel(this.customer);			
	var oBindingContext = oEvent.getSource().getBindingContext();
	var oCustomerModel = sap.ui.getCore().getModel('/CustomerID');
	},
onInit: function() {
	this.customer = new sap.ui.model.json.JSONModel();
	this.mBindingOptions = {};
	this.oRouter = sap.ui.core.UIComponent.getRouterFor(this);
	this.oRouter.getTarget("Customer").attachDisplay(jQuery.proxy(this.handleRouteMatched, this));
}

And in the Second (receiving) Controller is:

onAddPressed: function(oEvent) {
	var oCustomerNumber = sap.ui.getCore().getModel("/CustomerID");
}

When I stop on the first line in the receiving Controller, oCustomerNumber is undefined.

Am I forgetting to do something?

Accepted Solutions (1)

Accepted Solutions (1)

irfan_gokak
Contributor

Hi,

In First controller set value like this.

ap.ui.getCore().setModel(this.customer, "MdlName");

At receiving controller like this.

sap.ui.getCore().getModel("MdlName");

Try this

ianfmccallum
Explorer
0 Kudos

Thanks, was missing the model name...

Answers (3)

Answers (3)

UxKjaer
Product and Topic Expert
Product and Topic Expert

You shouldn't really set data in the core. Why don't you use routing?

Have a look here at this sample

ianfmccallum
Explorer
0 Kudos

It isn't my preference, but seem to be an expedient way to accomplish what I needed (passing a value from one view to another). If I read the code in the sample correctly, you can pass parameters when routing, is that true? (I am new to SAP UI5). I will try to find some additional tutorials, but that seems like a good approach, too!

former_member251534
Participant

Hi Ian ,

Write below code in second controller . This would return all the data that you want in this controller

var oCustomerNumber = sap.ui.getCore().getModel( );
 var aData = oCustomerNumber.getData();
ianfmccallum
Explorer
0 Kudos

This got the value, thanks...

former_member516423
Participant
0 Kudos

Hello Ian,

Are you sure this line works? Have you checked the returning value?

var oCustomerID = this.getView().byId("CustomerID").getText();

I assume that the element assigned by CustomerID is sap.ui.commons.TextField. Hence, you should change your code to

var oCustomerID = this.getView().byId("CustomerID").getValue();

Regards,

Tri

ianfmccallum
Explorer
0 Kudos

I will correct that, also. Thanks, Tri!

Edit; turns out that 'CustomerID' is a (hidden) label, so getValue() doesn't work.