开发者

Passing values from view to controller

开发者 https://www.devze.com 2023-03-08 06:05 出处:网络
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 speci

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>
 开发者_如何学C   <h2>    
        <% using (Html.BeginForm())
            { %>          
             <label for="txtSearch"> Search: </label>
             <%: Html.TextBox("txtSearch", Model.Search) %>
             <% } %>
    </h2>


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() }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号