将值从视图传递到控制器

时间:2011-05-23 15:43:46

标签: asp.net asp.net-mvc-2 jqgrid

我有一个问题,我从文本框中读取值并通过URL将其值从jqgrid传递给控制器​​。如果文本框的值很简单,但是如果它以空格或任何特殊字符结尾,它似乎没有传递任何关于为什么会发生这种情况的想法?我在这里使用了示例,在所提到的情况下,值#txtSearch没有作为id传递给控制器​​。

      <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>

1 个答案:

答案 0 :(得分:1)

那是因为你应该对它进行正确的URL编码:

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

但我认为最好将它作为查询字符串而不是url路径传递,如果它将包含特殊符号:

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

或者如果您使用POST,请将其发送为postData

url: 'JSONData/SearchGUIString',
postData: { query: $('#txtSearch').val() }