Home > Backend Development > C++ > How to Properly Format jqGrid Dropdown Responses from ASP.NET MVC $.post Calls?

How to Properly Format jqGrid Dropdown Responses from ASP.NET MVC $.post Calls?

Susan Sarandon
Release: 2025-01-11 09:07:41
Original
956 people have browsed it

How to Properly Format jqGrid Dropdown Responses from ASP.NET MVC $.post Calls?

ASP.NET MVC and jqGrid drop-down list data formatting: solving $.post calling problem

In ASP.NET MVC you can dynamically populate jqGrid's edit data dropdown list. However, you may encounter issues with "undefined" values ​​appearing in dropdown lists. This may be caused by jqGrid's data formatting.

Problem Overview

When making a $.post call using jQuery, the data received from the controller may contain double quotes. This causes problems with jqGrid because it expects the value of the dropdown to be in the format "value:value;...".

Solution: Use ContentResult

To solve this problem, use ContentResult method instead of Json. The syntax of ContentResult is as follows:

<code class="language-csharp">return Content(sb.ToString());</code>
Copy after login

Alternative: use dataUrl and buildSelect

Another option is to use dataUrl and buildSelect attributes in jqGrid's editoptions or searchoptions. Using dataUrl, you provide a URL that returns data in the format required by jqGrid's select element. buildSelect is a custom function that receives data and returns the HTML of the select element.

Code Example

Here is an example of how to use dataUrl and buildSelect:

Controller operations:

<code class="language-csharp">public JsonResult GetDestinationList() {
    List<string> allDestinations = GetAllDestinations();
    return Json(allDestinations, JsonRequestBehavior.AllowGet);
}</code>
Copy after login

jqGrid options:

<code class="language-javascript">{ name: 'destinations', editable: true, edittype:'select',
  editoptions: { dataUrl: '/YourController/GetDestinationList', buildSelect: function(data){
    var s = '<select>';
    if (data && data.length) {
        for (var i = 0, l=data.length; i<l ; i++) {
            var ri = data[i];
            s += '<option value="' + ri + '">' + ri + '</option>';
        }
    }
    s += '</select>';
    return s;
  }}
}</code>
Copy after login

Summary

You can provide the correct data format for jqGrid dropdown values ​​from $.post calls in ASP.NET MVC by using the ContentResult method or the dataUrl and buildSelect properties.

The above is the detailed content of How to Properly Format jqGrid Dropdown Responses from ASP.NET MVC $.post Calls?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template