Question: Mvc Autogenerate Html Button. Model Or Controller?

This relates to what is best practice with regards to MVC.

I have a snippet of code which auto-generates an AJAX HTML button which looks up a value from the database and prompts the user to overwrite what is in the current field on their HTML form. The arrangement works. But want to try to follow correct conventions. The parts are:

  1. A function which is passed the name of a field and uses CHtml::ajaxButton to return an HTML button. I echo the result on the _form.php view. There is javascript involved and I wanted to reuse the code rather than write it out 10 times separately for each button on the _form.php page. This just generates buttons. (Currently in the model)

  2. A function which handles the AJAX request. (Currently in the controller.)

  3. A function which (2) looks to to actually get the value from the database. (I split this off an put it into the model)

Question:

i) Model View or Controller for the generate HTML button function? It does not need to be used anywhere else in the app just many times on this one form.

ii) Is there a more elegant way to handle what I have done by splitting the function into 2 parts to handle the AJAX request. This way end up with a very skimpy function in the controller which just asks a function in the model for the value from the database and then displays it as view with no layout for the AJAX. Seems wrong to put it all in the controller.

Thanks for your wisdom if you have any input here.

if I understand you want to create a common button to all _form?

For one particular product table there are about 20 fields. For most of the fields I want to create a button next to them on the _form which does a look up and returns some value automatically into the field. Re-using same button on the _form for that table. Not for others.

After searching through how other people solved similar things in the past on the Yii forum it seems that what is missing is my use of components.

  1. The function that auto-generates HTML buttons and Javascript code can be made into a component and thus re-used in multiple places in the application.

  2. The function which deals with the actual AJAX request goes into the controller for the table it is querying.

  3. The actual database look-up and query used goes into the model.