how to pass parameter from @Url.Action to controller function

后端 未结 10 766
一生所求
一生所求 2020-12-02 19:42

I have a function CreatePerson(int id) , I want to pass id from @Url.Action.

Below is the reference code:

pub         


        
相关标签:
10条回答
  • 2020-12-02 20:22
    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));
    
    0 讨论(0)
  • 2020-12-02 20:23

    you can pass it this way :

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

    OR can also pass this way

    Url.Action("CreatePerson", "Person", new { id = id });
    
    0 讨论(0)
  • 2020-12-02 20:28

    If you are using Url.Action inside JavaScript then you can

    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!");
      }
    });
    
    0 讨论(0)
  • 2020-12-02 20:32

    Try this

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

    you will get the id inside the Enc string.

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