【问题标题】:how to pass parameter from @Url.Action to controller function如何将参数从@Url.Action 传递给控制器​​函数
【发布时间】:2012-10-22 18:31:30
【问题描述】:

我有一个函数CreatePerson(int id),我想从@Url.Action 传递id

以下是参考代码:

public ActionResult CreatePerson(int id) //controller 
window.location.href = "@Url.Action("CreatePerson", "Person") + id";

上述代码未能将id 值传递给CreatePerson 函数。

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    你可以这样传递:

    Url.Action("CreatePerson", "Person", new RouteValueDictionary(new { id = id }));
    

    OR也可以这样通过

    Url.Action("CreatePerson", "Person", new { id = id });
    

    【讨论】:

    • 如果代码在javascript里面怎么办? window.refresh({url: '@Url.Action("CreatePerson", "Person", new { id = Id })', -> 这段代码中断,因为我无法在其中传递变量。仅在硬编码时才有效:@Url.Action("CreatePerson", "Person", new { id = "someId" })', -> 如何使其工作?
    • 我也在寻找这个答案
    • 如果函数在 javascript 文件中,您可以将 url 放在视图中的隐藏字段中,然后使用 javascript 调用它。 eg: @Url.Action("CreatePerson", "Person", new { id = Id }) 然后在javascript中调用它: $("#url").val( );
    • 这就是我所做的: var url = '@Url.Action("Foo","Bar", new {id="replaceToken"})';然后 url = url.replace("replaceToken", yourID);
    • @chiapa 我知道我正在回复一个很长的死评论,但无论如何,将这些作为参数单独传递而不是整个 Razor 命令更容易,如在new { action = "CreatePerson", controller = "Person", id = Id } 中,然后在你的操作中重定向到作为参数接收到的操作RedirectToAction(action, controller, id)
    【解决方案2】:
    public ActionResult CreatePerson (string id)
    
    window.location.href = '@Url.Action("CreatePerson", "Person" , new {id = "ID"})'.replace("ID",id);
    
    public ActionResult CreatePerson (int id)
    
    window.location.href = '@Url.Action("CreatePerson", "Person" , new {id = "ID"})'.replace("ID", parseInt(id));
    

    【讨论】:

      【解决方案3】:
      public ActionResult CreatePerson(int id) //controller 
      
      window.location.href = '@Url.Action("CreatePerson", "Person")?id=' + id;
      

      或者

      var id = 'some value';
      window.location.href = '@Url.Action("CreatePerson", "Person", new {id = id})';
      

      【讨论】:

        【解决方案4】:

        试试这个:

        public ActionResult CreatePerson(int id) //controller 
        
        window.location.href = "@Url.Action("CreatePerson", "Person")?Id='+Id+'";
        

        它可以正常传递参数。

        【讨论】:

          【解决方案5】:

          这种方式将值从Controller传递到View:

          ViewData["ID"] = _obj.ID;
          

          这是将值从 View 传递回 Controller 的方法:

          <input type="button" title="Next" value="Next Step" onclick="location.href='@Url.Action("CreatePerson", "Person", new { ID = ViewData["ID"] })'" />
          

          【讨论】:

            【解决方案6】:

            需要两个或更多参数将视图传递给控制器​​使用此语法... 试试吧。。

            var id=0,Num=254;var str='Sample';    
            var Url = '@Url.Action("ViewNameAtController", "Controller", new RouteValueDictionary(new { id= "id", Num= "Num", Str= "str" }))'.replace("id", encodeURIComponent(id));
                Url = Url.replace("Num", encodeURIComponent(Num));
                Url = Url.replace("Str", encodeURIComponent(str));
                Url = Url.replace(/&amp;/g, "&");
            window.location.href = Url;
            

            【讨论】:

              【解决方案7】:

              试试这个

              public ActionResult CreatePerson(string Enc)
              
               window.location = '@Url.Action("CreatePerson", "Person", new { Enc = "id", actionType = "Disable" })'.replace("id", id).replace("&amp;", "&");
              

              您将在 Enc 字符串中获得 id。

              【讨论】:

              • 谢谢它的工作.... .replace("id", encodeURIComponent(id)) 这也是必需的.....
              【解决方案8】:

              你应该这样通过吗:

              public ActionResult CreatePerson(int id) //controller 
              window.location.href = "@Url.Action("CreatePerson", "Person",new { @id = 1});
              

              【讨论】:

                【解决方案9】:
                @Url.Action("Survey", "Applications", new { applicationid = @Model.ApplicationID }, protocol: Request.Url.Scheme)
                

                【讨论】:

                  【解决方案10】:

                  如果你在 JavaScript 中使用 Url.Action 那么你可以

                  var personId="someId";
                  $.ajax({
                    type: 'POST',
                    url: '@Url.Action("CreatePerson", "Person")',
                    dataType: 'html',
                    data: ({
                    //insert your parameters to pass to controller
                      id: personId 
                    }),
                    success: function() {
                      alert("Successfully posted!");
                    }
                  });
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2014-09-09
                    • 2015-01-01
                    • 2015-04-10
                    • 2016-08-22
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多