ActiveDropDownList中的默认值

I've inherited a piece of code from an old developer and currently trying to figure out the method behind how it submits.

I'm currently trying to get a quick fix in place where I hide one of his fields to get the form submitting, the issue I'm having is that I can't seem to set the dropdownlist to a default value, which should be United Kingdom, with the id 826 in the database.

echo CHtml::ActiveDropDownList($address, 'country_id', CHtml::listData(Country::model()->findAll(), 'id', 'name', 'continent'), array(
    'class' => 'col-md-5 hidden',
    'prompt' => 'Select Country',
    'label' => 'something here',
    'ajax' => array(
        'type' => 'POST',
        'url' => CController::createUrl('/user/UpdateRegions'),
        'dataType' => 'json',
        'data' => array('country_id' => 'js:this.value', 'YII_CSRF_TOKEN' => Yii::app()->request->csrfToken),
        'success' => 'function(data) {
                            $("#Address_country_region_id").html(data.country_id);
                            $("#Address_country_region_id").removeClass(\'hidden\');

                            if($("#venue_id").val() === "") {
                                $("#Address_country_region_id").addClass(\'hidden\');
                                }
                        }',
)));

$path = CController::createUrl('/admin/user/UpdateRegions');
$id = $address->id;
// On Load
Yii::app()->clientScript->registerScript('ready', '
    $.ajax({
        type: \'POST\',
        dataType : \'json\',
        data: {\'country_id\': $(\'#Address_country_id\').val(), \'update_id\' : "' . $id . '"},
        url: "' . $path . '",
        success: function(data){
                    $(\'#Address_country_region_id\').html(data.country_id);
                }
    })
');

How do I get this dropdownlist pointing towards the country_id of 826 as the page loads so I can hide the field and pass the validation of the form.

regards.

this code you pasted does this:

the first part renders a dropdown which does an ajax request on change to /user/UpdateRegions to fill the dependent dropdown for the regions.

the second part does also an ajax request when the page is "ready". So that the region select is filled when the page is ready.

Your script should output the id 826 if you printout the current set id. If not the value isn't correctly set to the address model.

echo $address->country_id;

If you want to set the default value you can do this at multiple places. e.g. in the controller 'before' the actual submitted value is set or in the model code itself (depends on your code e.g. if you have an additional form model which is extended from your activerecord model).

The correct line in your controller could be before something like this:

 $address->country_id= 826;
 before one of these lines in your controller

 $address->attributes=Yii::app()->request->getQuery(get_class($address));
 or 
 $address->attributes=Yii::app()->request->getPost(get_class($address));
 or 
 $address->attributes=$_POST['address']; // or $_GET

If you have difficulties to find the correct position post some controller code.

I guess you could find some help also here Yii 1.1: An Easy Solution for Dependent dropDownList Using AJAX

To set default value for $address model, put

$address->country_id = Yii::app()->request->getParam(get_class($address).'[country_id]', 816);

before

echo CHtml::ActiveDropDownList($address, 'country_id', CHtml::listData(Country::model()->findAll(), 'id', 'name', 'continent'), array(