Url.Action 参数? [英] Url.Action parameters?

查看:38
本文介绍了Url.Action 参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的列表控制器中,

 public ActionResult GetByList(string name, string contact)
 {        
     var NameCollection = Service.GetByName(name);    
     var ContactCollection = Service.GetByContact(contact);           
     return View(new ListViewModel(NameCollection ,ContactCollection));
 }

在我调用的 ASPX 页面中,

In ASPX page I call,

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

我的 ASPX 代码有问题...我可以提取姓名 john 的记录.但是当我给 contact="calgary, vancouver" 时,网页会出错.

I have a problem in the ASPX code... I can pull the records for the name john. but when I give the contact="calgary, vancouver", the webpage goes to error.

如何调用Url.Action 中的两个参数.我尝试了以下但似乎也错了.

How can I call two parameters in the Url.Action. I tried the below but that seems wrong too.

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

推荐答案

以下是正确的重载(在您的示例中,您缺少 routeValues 的关闭 }匿名对象,因此您的代码将引发异常):

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>

这将成功调用传递两个参数的 GetByList 控制器操作:

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

public ActionResult GetByList(string name, string contact) 
{
    ...
}

这篇关于Url.Action 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆