@ Html.ActionLink与链接一个id [英] @Html.ActionLink with an id for the link

查看:101
本文介绍了@ Html.ActionLink与链接一个id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC像(我用剃刀使用MVC C#):

I have something like in MVC (I am using MVC c# using razor):

@Html.ActionLink("Edit", "EditProject", new { id = item.ProjectID}) 

产生像我看看源$ C ​​$ C:

produces something like when I look at the source code:

<a href="/iDVI/Project/EditProject/535">Edit</a>

请注意如何535是有item.ProjectID的结果。
这可以。我想,虽然做的是把一个id为这个编辑链接。是这样的:

Note how 535 was the result of having item.ProjectID. This is fine. What I like to do though is to put an id for this edit link. Something like:

<a href="/iDVI/Project/EditProject/535" id="Projectid">Edit</a>

我如何做到这一点。我知道使用新的{ID = ...}将创建一个ID,但和id已经用来做什么我intially需要做的。

How do I do this. I know using new{id=...} will create a id but and id is already used for what I intially needed to do.

我现在需要给一个id的链接?

I need to give an id to the link now?

推荐答案

您必须在方法的签名看,当你使用它。
当你写的:

You have to look at the signature of the method when you are using it. When you wrote:

@Html.ActionLink("Edit", "EditProject", new { id = item.ProjectID}) 

您使用以下过载:

HtmlHelper.ActionLink(字符串LINKTEXT,串ActionName,对象
  routeValues​​)

所以这就是为什么你让你的item.ProjectID作为URL的一部分。正是在这种情况下,一个routeValue

So this is why you were getting your item.ProjectID as a part of the url. It is a routeValue in this case.

这是你正在寻找的超载可能是这样的:

The overload that you are looking for is probably this one:

HtmlHelper.ActionLink(字符串LINKTEXT,串ActionName,对象
  routeValues​​,对象htmlAttributes)

使用此重载您指定要使用你的变量item.ProjectID作为网址的一部分,以及链接的属性:

Using this overload you specify that you want to use your variable item.ProjectID as a part of url as well as an attribute of the link:

@Html.ActionLink("Edit", "EditProject", new { id = item.ProjectID}, new { id = item.ProjectID })

如果你真的想拼出变量的名称,你可以这样写:

If you actually want to spell out the name of the variable, you would write:

@Html.ActionLink("Edit", "EditProject", new { id = item.ProjectID}, new { id = @"Projectid" })

这为您提供了所需的输出。

This gives you the desired output.

这篇关于@ Html.ActionLink与链接一个id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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