Home > Backend Development > C++ > Why Does My ASP.NET MVC $.post Request Return Unexpectedly Formatted Strings for jqGrid Dropdowns?

Why Does My ASP.NET MVC $.post Request Return Unexpectedly Formatted Strings for jqGrid Dropdowns?

Mary-Kate Olsen
Release: 2025-01-11 07:23:42
Original
426 people have browsed it

Why Does My ASP.NET MVC $.post Request Return Unexpectedly Formatted Strings for jqGrid Dropdowns?

String formatting issues with ASP.NET MVC and jqGrid drop-down menu

Overview

When using jqGrid to dynamically populate the drop-down menu for data editing, additional "undefined" items will appear in the drop-down menu. This issue stems from the string format used to populate the dropdown menu values.

Problem Analysis

jqGrid requires the drop-down menu value format to be as follows:

<code>value: "FE:FedEx; IN:InTime; TN:TNT"</code>
Copy after login

However, the code in the ASP.NET MVC action generates the string using sb.ToString(), which adds unnecessary quotes around the value:

<code>value: ""ID: One;ID: Two;ID: Three;ID: Four;ID: Five;""</code>
Copy after login

Solution

Method 1: Use dataUrl

In order to solve this problem, it is recommended to use the dataUrl attribute of editoptions or searchoptions in jqGrid. This allows you to specify a URL that returns results in the desired format:

<code>{ name: 'destinations', ditable: true, edittype:'select',
  editoptions: { dataUrl:'<%= Url.Action("GetDestinationList","Home") %>' }
}</code>
Copy after login

In the controller, the GetDestinationList operation should return a JSON array containing the dropdown menu values:

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

Method 2: Use the buildSelect function

If dataUrl is not available, you can use the buildSelect function to format the drop-down menu value:

<code class="language-javascript">                 buildSelect: function(data) {
                     var s = '';
                     if (response && response.length) {
                         for (var i = 0, l=response.length; i<l ; i++) {
                             var ri = response[i];
                             s += ''+ri+'';
                         }
                     }
                     return s + "";
                 }</code>
Copy after login

This function receives the response data from the server and returns a string in the required format.

The above is the detailed content of Why Does My ASP.NET MVC $.post Request Return Unexpectedly Formatted Strings for jqGrid Dropdowns?. 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