views:

29

answers:

1

I am trying to send a Dictonary key (which is a string) to a Javascript function.

<%
    foreach (var field in Model.Fields)
    { %>
      <tr><td>
      <a href="#" onclick="javascript:EditField(<%= field.Key %>)">
      <%= Html.Encode(field.Value.Name) %></a>
      </td><tr>
<% } %>

But, on the Javascript function, I get it as an object which has the entire 'FIELD' object that I have. I don't get it as a string.

My JS function looks like this -

function EditField(field) {
// blah blah

}

Are there any 'gotchas' when sending Dictonary keys to a JS function?

A: 

You could pass only simple types such as strings and integers to a javascript function. If you want to pass complex objects you will need to JSON encode them. In your case you didn't mention the .NET type of your dictionary key, but if it is string you need to put it between quotes and don't forget to HTML encode it:

<a href="#" onclick="javascript:EditField('<%= Html.Encode(field.Key) %>')">
Darin Dimitrov