0

I have an issue where I read the value from textbox and pass its value through URL from a jqgrid to controller. It works if the value of the textbox is simple, but if it ends with a space or any special character it doesn't appear to be passed any idea's on why this would happen? I have the sample here that I am using, the value #txtSearch is not being passed to controller as id under the mentioned cases.

      <script type="text/javascript">

    $(function () {
        jQuery("#list").jqGridCustom({
            url: 'JSONData/SearchGUIString/' + $('#txtSearch').val(),                
         Model.Search }) %>',
            datatype: 'json',
            colNames: [ 'Results', 'Reference ID', 'Location'],
            colModel: [
                { name: 'Results', index: 'results', width: 40, align: 'left', sortable: false },
                { name: 'Reference ID', edittype: 'select', formatter: 'showlink', formatoptions: { baseLinkUrl: '<%= Url.Action("EditSearchResults", new {controller = "Search"}) %>', addParam: '&action=edit' }, width: 40, align: 'left', sortable: false },
                { name: 'Location', index: 'fileLocation', width: 200, align: 'left', sortable: false }, ],
            pager: $('#pager'),
            autowidth: true,
            rowNum: 20,
            height: "345",
            rowList: [5, 10, 20, 50],
            recordtext: "View Records {0} - {1} of {2}",
            emptyrecords: "No records to view",
            loadtext: "Loading...",
            pgtext: "Page {0} of {1}",
            sortname: 'Results',
            sortorder: "desc",
            viewrecords: true,
            scroll: false,
            loadonce: false,
            caption: 'Search Results'
        });
    });        
     </script>
    <h2>    
        <% using (Html.BeginForm())
            { %>          
             <label for="txtSearch"> Search: </label>
             <%: Html.TextBox("txtSearch", Model.Search) %>
             <% } %>
    </h2>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
remo
  • 3,326
  • 6
  • 32
  • 50

1 Answers1

1

That's because you should properly URL encode it:

url: 'JSONData/SearchGUIString/' + encodeURIComponent($('#txtSearch').val())

But I think it would be better to pass it as query string instead of the url path if it is going to contain special symbols:

url: 'JSONData/SearchGUIString?query=' + encodeURIComponent($('#txtSearch').val())

or if you are using POST send them as postData:

url: 'JSONData/SearchGUIString',
postData: { query: $('#txtSearch').val() }
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    @sharma & @Darin: I want only add one more syntax for the `postData` usage: `postData: { query: function() { return $('#txtSearch').val(); } }`. The advantage of the way is that the function which returns the value of the `query` property will be called **on every grid refresh** (changing of the page, sorting...) and not only during the grid initialization. So the value from the `#txtSearch` (`$('#txtSearch').val()`) will be every time **the current**. See [here](http://stackoverflow.com/questions/2928371/how-to-filter-the-jqgrid-data-not-using-the-built-in-search-filter-box/2928819#2928819). – Oleg May 23 '11 at 16:57