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

后端 未结 10 765
一生所求
一生所求 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:05

    Need To Two Or More Parameters Passing Throw view To Controller Use This Syntax... Try.. It.

    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(/&/g, "&");
    window.location.href = Url;
    
    0 讨论(0)
  • 2020-12-02 20:07

    should you pass it in this way :

    public ActionResult CreatePerson(int id) //controller 
    window.location.href = "@Url.Action("CreatePerson", "Person",new { @id = 1});
    
    0 讨论(0)
  • 2020-12-02 20:12
    @Url.Action("Survey", "Applications", new { applicationid = @Model.ApplicationID }, protocol: Request.Url.Scheme)
    
    0 讨论(0)
  • 2020-12-02 20:13

    Try this:

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

    it's working fine passing parameter.

    0 讨论(0)
  • 2020-12-02 20:16

    This way to pass value from Controller to View:

    ViewData["ID"] = _obj.ID;
    

    Here is the way to pass value from View to Controller back:

    <input type="button" title="Next" value="Next Step" onclick="location.href='@Url.Action("CreatePerson", "Person", new { ID = ViewData["ID"] })'" />
    
    0 讨论(0)
  • 2020-12-02 20:17
    public ActionResult CreatePerson(int id) //controller 
    
    window.location.href = '@Url.Action("CreatePerson", "Person")?id=' + id;
    

    Or

    var id = 'some value';
    window.location.href = '@Url.Action("CreatePerson", "Person", new {id = id})';
    
    0 讨论(0)
提交回复
热议问题