Url.Action parameters?

前端 未结 4 1092
野性不改
野性不改 2020-11-28 04:42

In listing controller I have,

 public ActionResult GetByList(string name, string contact)
 {        
     var NameCollection = Service.GetByName(name);    
          


        
相关标签:
4条回答
  • 2020-11-28 05:22

    This works for MVC 5:

    <a href="@Url.Action("ActionName", "ControllerName", new { paramName1 = item.paramValue1, paramName2 = item.paramValue2 })" >
        Link text
    </a>
    
    0 讨论(0)
  • 2020-11-28 05:23

    Here is another simple way to do it

    <a class="nav-link"
       href='@Url.Action("Print1", "DeviceCertificates", new { Area = "Diagnostics"})\@Model.ID'>Print</a>
    

    Where is @Model.ID is a parameter

    And here there is a second example.

    <a class="nav-link"
       href='@Url.Action("Print1", "DeviceCertificates", new { Area = "Diagnostics"})\@Model.ID?param2=ViewBag.P2&param3=ViewBag.P3'>Print</a>
    
    0 讨论(0)
  • 2020-11-28 05:24

    you can returns a private collection named HttpValueCollection even the documentation says it's a NameValueCollection using the ParseQueryString utility. Then add the keys manually, HttpValueCollection do the encoding for you. And then just append the QueryString manually :

    var qs = HttpUtility.ParseQueryString(""); 
    qs.Add("name", "John")
    qs.Add("contact", "calgary");
    qs.Add("contact", "vancouver")
    
    <a href="<%: Url.Action("GetByList", "Listing")%>?<%:qs%>">
        <span>People</span>
    </a>
    
    0 讨论(0)
  • 2020-11-28 05:31

    The following is the correct overload (in your example you are missing a closing } to the routeValues anonymous object so your code will throw an exception):

    <a href="<%: Url.Action("GetByList", "Listing", new { name = "John", contact = "calgary, vancouver" }) %>">
        <span>People</span>
    </a>
    

    Assuming you are using the default routes this should generate the following markup:

    <a href="/Listing/GetByList?name=John&amp;contact=calgary%2C%20vancouver">
        <span>People</span>
    </a>
    

    which will successfully invoke the GetByList controller action passing the two parameters:

    public ActionResult GetByList(string name, string contact) 
    {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题