cancel
Showing results for 
Search instead for 
Did you mean: 

MVC data binding dynamically

Former Member
0 Kudos

Hi Friends,

I have a requirement in one of my MVC views. I have an <htmlb:inputField> and i want to associate it to my Model class attribute dynamically i.e. i have the fully typed model attribute in a string and i wish to assign it to the inputField's value. But, my efforts are in vain.

Here's my code:

<% concatenate '//model/structure.' tmp into substr. %>

<htmlb:inputField id = "<%= tmp %>"

value = "<%= substr %>" />

The 'tmp' has the name of the structure component in it. The code gets compiled but when i run it the inputField has the whole model string (substr) as text inside the input box and is not actually getting binded with the model. How can we dynamically do this ?

Please help.

Regards,

Ram.

Accepted Solutions (0)

Answers (1)

Answers (1)

raja_thangamani
Active Contributor
0 Kudos

System will consider '//model/structure.' as the value. no way it can recogize this as the model binding. If you want to display the dynamically then you achieve as follows:

<% substr = model->structure-field1. %>

<htmlb:inputField id = "<%= tmp %>"
value = "<%= substr %>" />

This way, the substr will display the value of model attribute.

Raja T

Former Member
0 Kudos

Hi Raja,

Thanks for the reply. But, my requirement is that i am iterating through a loop and creating a htmlb:inputField for each iteration dynamically. During this creation process, i want to attach this inputField to a component in a model attribute (structure).

Is there any way i can do this when i have the name of in a string variable (model-attribute-component) ??

raja_thangamani
Active Contributor
0 Kudos

Ram,

You can display the values as well get the modified values.

<% substr = model->structure-field1. %> 

<% concatenate tmp 'model_structure_field1'  into tmp.% >

<htmlb:inputField id = "<%= tmp %>"
value = "<%= substr %>" />

As long as u can display & get the modified value back you shd be fine. let me know if you are looking for more..

<b>*Reward each useful answer</b>

Raja T

Former Member
0 Kudos

Hi Raja,

Your solution was useful. I used Field-Symbols to construct the necessary model attribute's component in the below way:

assign modelRef->(substr) to <fs>. "substr has the attributeName-componentName

<htmlb:inputField id="id1"

value="<%= <fs> %>" />

I am able to bind the inputField to the model's attribute i want to and i am able to display the model attribute's value in the inputField. But, the other way is not happening. The modified values in the inputField is not getting updated in the model's attribute.

Any idea why this is not happening ??

Patrick_McMahon
Contributor
0 Kudos

Hi Ram,

If you want your values to be updated you would need to code this in the DO_HANDLE_DATA method using Raja's code.

You cannot dynamically call the data binding in the BSP layout page; the "//model/attribute" must be declared at design time. However, you could try experimenting with a model attribute of type ANY.

Regards,

Patrick.

raja_thangamani
Active Contributor
0 Kudos

Good thought..

To do otherway around, make sure that your inputfield ID should have the naming format <i>model_structure_field1</i>. If you use ID as id1, then you need to use get_form_fields method to get. But this is extra effort. Try the previous option which i mentioned.

Raja T

Former Member
0 Kudos

Hi Raja/Patrick,

Could you please brief me in detail abt the solutions you proposed ? Since, i am out of office, i would be able to implement the solution and check only in the morning (Time zone diff. screws up and i can't take the liberty of disturbing your sleep ).

I am able to understand that on the page layout, i cannot attain the dynamic binding. So, how can i go about in the DO_HANDLE_DATA. Please guide. A code snippet might surely help me. Also, i didn't quite get trying a model attribute of TYPE ANY.

Thanks,

Ram.

raja_thangamani
Active Contributor
0 Kudos

Ram,

What I meant is, if you name the inputfield with ID as simply ID1, it will not get the modified value simply. It shd have the following naming convention <i>MODEL_STRUCURE_FIELDNAME</i>.

Say you want to display model->struc-carrid then your inputfield id shd be <i>model_struc_carrid</i>.

you can dynamically populate this into tmp and show as below:

<htmlb:inputField id = "<%= tmp %>"

Hope this will give you clear idea.

<i>*Reward each useful answer</i>

Raja T

Former Member
0 Kudos

Hi Raja,

I could understand your point. So, if i have the id of the inputField follow that naming convention and i already have the value = model->struc-field, i can achieve two way data binding. So, there will not be any need to dwell into DO_HANDLE_DATA. I guess i got it right. Please correct me if i am worng.

raja_thangamani
Active Contributor
0 Kudos

Yes you are correct.

follow the naming convention for ID.

If that doesnt work

Have below code in DO_HANDLE_DATA:

CALL METHOD SUPER->DO_HANDLE_DATA
  EXPORTING
    FORM_FIELDS     = FORM_FIELDS
    GLOBAL_MESSAGES = GLOBAL_MESSAGES

Automatically the modified data will be available in model->struc.

This way it always worked for me.

<b>*Reward each useful answer</b>

Raja T

Former Member
0 Kudos

Hi Raja,

In giving the inputField ID as 'model_structure_field', what has to be the value for model? I mean when i concatenate, do i need to use name of model ref or name of model class ??

Also, if i already have '_' in the names of model or structure or field name, will it affect the data binding ??

Message was edited by:

Ram Mohan Venkatraman

Former Member
0 Kudos

Anybody who can throw some light into this issue ?? Helpful solutions will surely be awarded !!

Thilo
Explorer
0 Kudos

Here is your solution: https://wiki.sdn.sap.com/wiki/display/BSP/Data+Binding

regards

Thilo

Former Member
0 Kudos

Thanks Thilo. Though not exactly the solution i needed, it surely guided me to find the right one.

Raja/Patrick and Everyone,

Luckily i found a relatively easy way to dynamically bind an htmlb element to a model attribute. Below code snippet says it all:

<% data: input type ref to cl_htmlb_inputField,

input_string type string.

create object input.

input->id = tmp.

input->_value = '//model/structure.field'.

input_string = input->if_bsp_bee~render_to_string( page_context ).

%>

<%= input_string %>

By putting this code on the page layout, the htmlb:inputField is dynamically created and during this process the 'value' is binded to the string provided. Please go through the PDF file for more details: https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/72d7ac11-0501-0010-63a2-cc696056...

Thanks everyone,

Ram.