Javascript url action in razor view

前端 未结 2 1268
情话喂你
情话喂你 2020-12-06 00:30

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

f         


        
相关标签:
2条回答
  • 2020-12-06 01:19

    The method by Darin will work fine and perfectly.

    I would suggest one more way for Razor view to use model value using @Html.actionlink in jsp

    var URLvalue='@Html.ActionLink("UserString", "action", new { routeValueName1 = "__value1__", routeValueName2="__value2__" }, htmlAttributes: new { @class = "btn btn-default", @role = "button" })'
         .replace('__value1__', Model.somevalue).replace('__value2__',  Model.somevalue);
    

    you can use URLvalue where ever you want to in jsp or jquery.

    0 讨论(0)
  • 2020-12-06 01:22

    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.

    0 讨论(0)
提交回复
热议问题