0

In my view, I have a javascript function to handle a select event on a pie chart. The function is shown below:

function selectHandler() {
    var selectedItem = visualization.getSelection()[0];
    if (selectedItem) {
        var val = data.getFormattedValue(selectedItem.row, 0);
        location.href = '/Tickets';
    }
}

Currently I am on the Home Controller in the Groups View. I want to navigate to the Index View of the Tickets Controller while passing the selected value from the javascript variable "val". How would I go about doing this?

4

4 回答 4

2

您是否打算手动导航用户?

如果您正在寻找一种重定向 JavaScript 方式,那么您可以做一些简单的事情...

location.href = '/Tickets?value=' + val;

现在这可能并不适用于所有事情。例如,如果 location.href 已经包含一个“?”,并且您需要维护该上下文,那么您需要使用“&”。也许您的应用程序存在于虚拟目录中。

你可能会做类似...

var newUrl = location.href;
if (newUrl.indexOf('?') > -1)
  newUrl += '&';
else
  newUrl += '?';

newUrl += val;

这也允许您维护任何现有的上下文。

如果您希望票证已经定义,您可能需要从查询字符串中删除它(如果它已经存在)。

在这种情况下,您可能想要做类似...

var params = location.search.substring(1).split('&'),
    paramToRemove, indexOfValue,
    hasSearch = false,
    param;
for (var i = 0, len = i; i < len; i++)
{
  param = params[i];
  indexOfValue = param.indexOf('value');
  hasSearch = param.indexOf('?') === 0;
  if (indexOfValue  === 0 || (indexOfValue === 1 && hasSearch ))
  {
    paramToRemove = params[i];
    break;
  }
}

var newUrl = location.href;
// Remove old value
if (paramToRemove) newUrl = newUrl.replace(paramToRemove, hasSearch ? '?' : '');

// Add proper search char
if (newUrl.indexOf('?') > -1)
  newUrl += '&';
else
  newUrl += '?';
// Add new value
newUrl += val;
于 2013-06-07T18:39:17.907 回答
1
location.href = '/Tickets?val=' + val;
于 2013-06-07T18:19:30.487 回答
1
//On page load the server will generate the URL for you.
var ticketURL = '@Url.Action("Index", "Tickets")';
//Append the value to the URL
ticketURL = ticketURL + '?val=' + val;
//Do your stuff!
于 2013-06-07T18:19:56.857 回答
0

因为,您正在从 javascript 调用 Controller 方法。您应该对 Ticket Controller 进行 POST ajax 调用并同时传递 Action 方法名称。

您的代码将是这样的:

return $.post('/Ticket(ControllerName)/Index(方法名)/',这里的参数);

在 API 控制器内部,Index 方法将接受我们从 javascript 传递的相同参数。

ActionResult 索引(参数){...}

于 2013-06-07T18:26:53.713 回答