简体   繁体   English

MVC 4将值从Javascript传递到控制器

[英]MVC 4 Pass value from Javascript to Controller

In my view, I have a javascript function to handle a select event on a pie chart. 在我看来,我有一个javascript函数来处理饼图上的select事件。 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". 我想导航到Tickets Controller的索引视图,同时传递从javascript变量“ val”中选择的值。 How would I go about doing this? 我将如何去做呢?

Are you intending to manually navigate the user? 您打算手动导航用户吗?

If you're looking for a redirect JavaScript way, then you would do something as simple as... 如果您正在寻找一种重定向JavaScript的方式,那么您可以做的简单...

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

Now this may not work for everything. 现在这可能不适用于所有情况。 For example, if location.href already contains a '?', and you need to maintain that context, then you need to use '&'. 例如,如果location.href已经包含“?”,并且您需要维护该上下文,则需要使用“&”。 Maybe your app lives in a Virtual Directory. 也许您的应用程序位于虚拟目录中。

You might do something like... 您可能会做类似...

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

newUrl += val;

This allows you maintain any existing context as well. 这也允许您维护任何现有上下文。

If you expect the ticket to already be defined, you might need to remove that from the query string, if it already exists. 如果希望已定义票证,则可能需要将其从查询字符串中删除(如果已存在)。

In that case then you might want to do something like... 在这种情况下,您可能想要做类似...

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;
location.href = '/Tickets?val=' + val;
//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!

Since, you are calling Controller methods from javascript. 从那开始,您就是从javascript调用Controller方法。 You should make an POST ajax call to Ticket Controller and passing Action method name also. 您应该对Ticket Controller进行POST ajax调用,并同时传递Action方法名称。

Your code would be like this: 您的代码将如下所示:

return $.post('/Ticket(ControllerName)/Index(method name)/',parameters here); 返回$ .post('/ Ticket(ControllerName)/ Index(方法名)/',这里的参数);

Inside API Controller, Index method will accept the same param which we are passing from our javascript. 在API控制器内部,Index方法将接受我们从JavaScript传递的相同参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM