FAQ for model binding topics, focussing specifically on how to use model binding with tags.
How to Bind Inputfields to a Structure Field?
Question: Is possible to assign values of inputfields directly to field of a structure? (Presently I have lots of inputfields on one view and these fields are corresponding to a line of a table which is defined on my model. To use the data binding, I create currently for each inputfield a model-attribute and than I use the usual way to write all values from these many model-attributes back to the table.)
Answer: Use a structure as an attribute of a model class, and then use the model binding relative to the structure in the view. Take care with the data binding syntax. Important is that strings are used to specify the model binding path. The string format is
//<model_name>/<structure_name>.<field>. This string always starts with a
//, then the name of the model attribut in the view follows. The attribute of the model comes behind a
'/' and the structure-element is separated by a
Why Can Model Binding Parameters Not Be Set Dynamically?
I use a <htmlb:inputField> with model binding and it works perfectly.
However, I would like to set the actual model binding string dynamically for many complex reasons. My code is now (with v = "//model/client.clientno"), which then displays the model binding string itself in the browser instead of the value behind it:
Answer: The problem is actually complex. When the compiler sees a tag, it generates code to execute it. The BSP compiler sees:
It generates code:
The problem is now what happens if the attribute is of type integer, or of type ref to data, and we have model binding. Then it is not possible to place the binding string inside the attribute (not matching types). For this reason, once model binding is set for a specific attribute, there are two generated attributes on the element. The one is of the configured type, the other (prefixed with _) is of type string for the binding path. Only one is set by the BSP compiler.
The BSP compiler sees:
It generates code (important, notice _a1):
Already at compile time, the compiler must know whether it must set a1 or _a1. Model binding is pure compile time construct, and does not work at runtime. It is not possible to set this parameter dynamically.
The only possible way to achieve this, if you really insist, is to dynamically process the tag directly on the page. Then you must set the attributes yourself correctly. For background reading information, I would recommend the weblog BSP Programming: Writing Composite Elements. The expected code will be about:
How to highlight inputfields containing wrong values?
I use a <htmlb:inputField> with model binding
In the methods of the modelI add a message to the errors object via method ADD_MESSAGE_FROM_T100.
Note: It's very important to set the value entered by the user in the method's parameter DUMMY otherwise the inputfiled will be displayed empty!