在剃刀鉴于JavaScript网址行动 [英] Javascript url action in razor view

查看:168
本文介绍了在剃刀鉴于JavaScript网址行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaScript方法 onRowSelected wchich得到的rowid。如何通过在控制器中的某些动作与ROWID HTTPGET

 函数onRowSelected(ROWID,状态){
        警报('这行有ID:'+ ROWID);
        //网址:@ Action.Url(动作,控制器)
        //帖子:GET
        //像这样的事情?
    }


解决方案

如果你的控制器动作需要一个ID查询字符串参数:

  VAR URL ='@ Url.Action(动作,控制器)ID ='+ ROWID;

或者,如果你想通过它,你可以使用替代路线的一部分:

  VAR URL ='@ Url.Action(动作,控制器,新{ID =_id_})
    .replace('_ ID_',ROWID);

但如果你要发送一个AJAX请求另一种可能性是将它传递作为POST身体的一部分:

  $。阿贾克斯({
    网址:'@ Url.Action(动作,控制器),
    输入:POST,
    数据:{ID:ROWID},
    成功:函数(结果){    }
});

或作为查询字符串参数,如果你使用的是GET:

  $。阿贾克斯({
    网址:'@ Url.Action(动作,控制器),
    输入:GET,
    数据:{ID:ROWID},
    成功:函数(结果){    }
});

所有这些假设你的控制器动作发生,当然id参数:

 公众的ActionResult动作(字符串ID)
{
    ...
}

因此​​,大家可以看到很多的方法来达到同样的目标。

I have a javascript method onRowSelected wchich gets rowid. How to pass the rowid in certain action of a controller with HttpGet?

function onRowSelected(rowid, status) {
        alert('This row has id: ' + rowid);
        //url: @Action.Url("Action","Controller")
        //post:"GET"
        // Something like this?
    }

解决方案

If your controller action expects an id query string parameter:

var url = '@Url.Action("Action", "Controller")?id=' + rowid;

or if you want to pass it as part of the route you could use replace:

var url = '@Url.Action("Action", "Controller", new { id = "_id_" })'
    .replace('_id_', rowid);

yet another possibility if you are going to send an AJAX request is to pass it as part of the POST body:

$.ajax({
    url: '@Url.Action("Action", "Controller")',
    type: 'POST',
    data: { id: rowid },
    success: function(result) {

    }
});

or as a query string parameter if you are using GET:

$.ajax({
    url: '@Url.Action("Action", "Controller")',
    type: 'GET',
    data: { id: rowid },
    success: function(result) {

    }
});

All those suppose that your controller action takes an id parameter of course:

public ActionResult Action(string id)
{
    ...
}

So as you can see many ways to achieve the same goal.

这篇关于在剃刀鉴于JavaScript网址行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆