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

查看:164
本文介绍了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参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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