Url.Action 参数?

编程入门 行业动态 更新时间:2024-10-24 16:25:43
本文介绍了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 参数?

本文发布于:2023-11-03 20:35:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556026.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   Url   Action

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!