0

I have an MVC2 EditStatesController :

     public JsonResult GetStates()
     {
         string statesToReturn = GetStates();  // returns "1: Alabama; 2: Alaska; 3:  Arizona; 4: Arkansas"
         return Json(statesToReturn);
     }

this is the code that calls the controller:

//get States
    var listOfStates = $.ajax({
        url:    '/EditStates/GetStates', 
        type: 'POST',
        async: false, 
        success: function(data, result) {
            if (!result) 
                alert('Failure to retrieve States.');
        }
    }).responseText;

The dropdown has the list of elements, but last element has extra " (double quote), so the last state Wyoming is Wyoming".

I searched other questions, but didn't find a similar one. Do you know why this is happening and how to fix this? Thank you, Jenny

Boroda
  • 195
  • 1
  • 7
  • 23
  • What dropdown are you talking about and how is it related to the code you have shown? What is the relation between your question and `jqgrid`? – Darin Dimitrov Jul 13 '11 at 20:45
  • dropdown I'm asking about is a dropdown on Edit form of jqgrid. It requires values for it to be in a specific format. It looks like at some point of processing - may be Json convertion adds extra quotes? – Boroda Jul 14 '11 at 14:17

1 Answers1

0

The searchoptions can use dataUrl and optionally buildSelect instead of value which you try to use currently.

jqGrid need to construct the HTML fragment like the following:

<select>
    <option value="1">Alabama</option>
    <option value="2">Alaska</option>
    <option value="3">Arizona</option>
    <option value="4">Arkansas</option>
</select>

So you can either provide the data from an action of your controller directly or provide any other output like JSON output:

[
    {"id":1, "name":"Alabama"},
    {"id":2, "name":"Alaska"},
    {"id":3, "name":"Arizona"},
    {"id":4, "name":"Arkansas"}
]

and use buildSelect event handler to convert the JSON data to the HTML fragment with <select>...</select>. See the answer for details.

If you choose the way you will have no problems with any special characters like '"', ':', ';' and so on.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks, but it seems that my solution worked except the quotes. I just found a solution to my problem here http://www.json.org/js.html. It looks like I needed to convert a JSON text into an object and do JSON.parse(listOfStates). That got rid of the quotes. – Boroda Jul 14 '11 at 15:31
  • @Boroda: Look at [the close answer](http://stackoverflow.com/questions/2981409/jqgrid-escape-in-searchoptions-value-part/3023128#3023128). The usage of the object instead of the string value for the `value` can solve some problems, but your current way have many disadvantages. The usage of `$.ajax` with `async: false` is always **very bad**. The `dataUrl` will be called asynchronous and only if it is needed. If the user for example don't open the search dialog and the dropdown don't need be built the `dataUrl` will be not called. I can continue... – Oleg Jul 14 '11 at 16:00
  • Why is it very bad to use $.ajax with async: false? I need to get data from the server. I'm not using searchoptions. Do I need to use searchoptions to create a drop down on the edit form? – Boroda Jul 15 '11 at 14:56