Url.Action参数?

编程入门 行业动态 更新时间:2024-10-24 18:28:57
本文介绍了Url.Action参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在列出的控制器我有,

公众的ActionResult GetByList(字符串名称,字符串触点) {     VAR NameCollection = Service.GetByName(名);     VAR ContactCollection = Service.GetByContact(接触);     返回查看(新ListViewModel(NameCollection,ContactCollection)); }

在ASPX页面我打电话,

< A HREF =<%:Url.Action(GetByList,上市,新的{名称=约翰},新{触点=卡尔加里,温哥华})%GT;><跨度>人与LT; / SPAN>< / A>

我在ASPX code的一个问题...我可以拉记录的名字John。但是当我给触点=卡尔加里,温哥华,网页去错误。

我如何可以调用Url.Action两个参数。我试过以下,但看上去是错了。

< A HREF =<%:Url.Action(GetByList,上市,新的{名称=约翰,触点=卡尔加里,温哥华} )%GT;><跨度>人与LT; / SPAN>< / A>

解决方案

下面是正确的过载(在你的榜样你缺少结束} 到 routeValues​​ 匿名对象,以便您的code会抛出异常):

< A HREF =<%:Url.Action(GetByList,上市,新的{名称=约翰,触点=卡尔加里,温哥华} )%>中与GT;    <跨度>人与LT; / SPAN>&所述; / A>

假设你使用的是默认路由这应该生成以下标记:

< A HREF =/上市/ GetByList名称=约翰&放大器;放大器;触点=卡尔加里%2C%20vancouver>    <跨度>人与LT; / SPAN>&所述; / A>

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

公众的ActionResult GetByList(字符串名称,字符串触点){    ...}

In listing controller I have,

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

In ASPX page I call,

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

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.

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>

解决方案

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) { ... }

更多推荐

Url.Action参数?

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

发布评论

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

>www.elefans.com

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