剃刀视图中的 Javascript url 操作 [英] Javascript url action in razor view

查看:23
本文介绍了剃刀视图中的 Javascript url 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 javascript 方法 onRowSelected 获取 rowid.如何使用 HttpGet 在控制器的某些操作中传递 rowid?

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

推荐答案

如果您的控制器操作需要一个 id 查询字符串参数:

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

如果您要发送 AJAX 请求,另一种可能性是将其作为 POST 正文的一部分进行传递:

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

    }
});

或者作为查询字符串参数(如果您使用的是 GET):

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

    }
});

所有这些都假设您的控制器操作当然采用 id 参数:

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 url 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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