【问题标题】:Additional parameter for index page sort collection索引页排序集合的附加参数
【发布时间】:2014-04-14 16:42:07
【问题描述】:

我有一个页面,它将问题对象呈现为一组<div>。 (我也使用分页,但我认为这对这个特定问题没有任何意义)。

我想让用户有机会选择项目的显示顺序(也就是基础问题列表的顺序。

我认为使用<select> 列表是一种简单的方法,但它似乎不起作用。我主要是在努力从我的控制器从 Javascript 调用一个动作。这是我目前所拥有的:

我的 HTML:

<select id="sort-select" style="margin-bottom: 10px">
    @foreach (SortingType value in Enum.GetValues(typeof (SortingType)).Cast<SortingType>())
    {
        <option value="@((byte)value)">@(value.ToString())</option>
    }
</select>

(SortingType 是一个包含 3 个常量的枚举。)

我的 JavaScript:

function AddSort() {
    var select = document.getElementById('sort-select');

    select.onchange = function(event) {
        var selected = select.options[select.selectedIndex].value;

        var url = encodeURI("@Url.Action("Index", new {topicId = ViewBag.TopicId, sort = 0})");
        url.replace('&amp;', '&');
        url.replace(0, selected);

        window.location.href = url;
    }
}

这当然会在 document.ready 中调用。

问题是,当 JS 运行时,它会将所有的 & 符号 (&) 转换为它们的转义等效值 (&)。当然这是行不通的,因为 Controller 的 Actions 的参数是由纯 & 号分隔的。

当我尝试更改选择时,我得到了这个链接:

http://localhost:41084/Question?topicId=-1&amp;sort=0

有人知道如何解决这个问题吗?我不确定如何以适当的方式从 Javascript 调用操作,我也不知道 JavaScript 是否适合这里。

【问题讨论】:

    标签: javascript html asp.net-mvc action


    【解决方案1】:

    发现解决方案必须使用url = url.replace(...) 而不仅仅是url.replace(...)

    我的 javascript 现在看起来像这样:

    function AddSort() {
        var select = document.getElementById('sort-select');
    
        select.onchange = function(event) {
            var selected = select.options[select.selectedIndex].value;
    
            var url = encodeURI("@Url.Action("Index", "Question", new {topicId = ViewBag.TopicId, sort = 0})");
            url = url.replace('&amp;', '&');
            url = url.replace(0, selected);
    
            window.location.href = url;
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      相关资源
      最近更新 更多