views:

21

answers:

1

Hello Friends, I have this code in my View..

<tr><td>Account:</td><td><%=Html.DropDownList("drdAccounts",Model.AccountsListHeader),Model.selectedAccount,"Select Account", new { onchange = "JavaScript:AccountChanged()" })%><span class="requiredAsterisk">*</span></td></tr>

But I am getting Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1002: ; expected

is that I am doing something wrong in this line? How to show the Selected account on the Dropdown list box? Thanks

+3  A: 

You have a closing parenthesis that you need to remove:

<%= Html.DropDownList("drdAccounts", Model.AccountsListHeader, 
    Model.selectedAccount, "Select Account", 
    new { onchange = "JavaScript:AccountChanged()" }
) %>

This being said I would recommend you using the strongly typed DropDownListFor helper method to generate dropdown lists:

<%= Html.DropDownListFor(x => x.selectedAccount, Model.AccountsListHeader, 
    "Select Account", new { id = "accounts" }) %>

And then use jquery to attach a change event handler unobtrusively:

$(function() {
    $('#accounts').change(function() {
        // TODO: do something when the current value changes
    });
});
Darin Dimitrov
I used strongly typed DropdownListFor but I am not able to show the Selected Account name in the Dropdown list box? Thanks
kumar
Simply set the `SelectedAccount` property on your view model to the desired value (value, not text) inside the controller action rendering this view.
Darin Dimitrov