Javascript Razor URL Action

I have a javascript method onRowSelectedwchich gets rowid. How to pass rowid on specific controller action with HttpGet?

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

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 a route that you can use, replace:

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

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 use GET:

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

    }
});

All of this assumes that your controller action, of course, accepts an identifier:

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

, .

+39

Source: https://habr.com/ru/post/1523563/


All Articles