[英]Pass model property to Html.ActionLink
Edit: I found the answer, but it meant abandoning the use of HtmlActionlink in favor of doing this (the "blah" parameters from my original post now being subbed in for what I actually needed): 编辑:我找到了答案,但这意味着放弃使用HtmlActionlink来做到这一点(我原来的帖子中的“ blah”参数现在根据我的实际需要被嵌入):
<a href="@Url.Action("LinkedDetails",
new
{
controller = "LGDetails",
findByString = item.AccounNumber
})">@item.LastName</a>
This does exactly what I want. 这正是我想要的。 Trying to accomplish the same thing with HtmlActionLink results in one error or another no matter what I tried:
无论我尝试了什么,尝试使用HtmlActionLink完成同一件事都会导致一个错误或另一错误:
@Html.ActionLink(item.LastName,
"../LGDetails/LinkedDetails",
new
{
controller = "LinkedDetails",
findByString = item.AccounNumber
})
result: Value cannot be null or empty. 结果:值不能为null或为空。 Parameter name: linkText
参数名称:linkText
Trying like this: 尝试这样:
@Html.ActionLink(Model.LastName .....
System.Collections.Generic.IEnumerable' does not contain a definition for 'LastName' and no extension method 'LastName' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)
System.Collections.Generic.IEnumerable'不包含'LastName'的定义,找不到扩展方法'LastName'接受类型为'System.Collections.Generic.IEnumerable'的第一个参数(是否缺少using指令或组装参考?)
etc etc. Always one of those errors regardless. 等等等等。无论如何总是这些错误之一。
And yes, I tried every single thing suggested here, and an equal number of other things found elsewhere (which is how I found the answer I did use, it was posted by another person having the same problem). 是的,我尝试了这里建议的每件事,并在其他地方发现了相同数量的其他事物(这是我发现使用的答案的方式,它是由另一个有相同问题的人发布的)。
Despite a lot of searching, I can't seem to figure out how to do what seems like it should be basic. 尽管进行了大量搜索,但我似乎无法弄清楚该怎么做似乎应该是基本的。
This works fine: 这很好用:
@Html.DisplayFor(modelItem => item.LastName)
But I want to display the LastName field as the linked text inside an Html.ActionLink like this: 但是我想将LastName字段显示为Html.ActionLink中的链接文本,如下所示:
@Html.ActionLink(item.LastName, "blah", "blah")
But nothing works. 但是什么都行不通。 Doing it like the above gives me a compilation error:
像上面那样做给我一个编译错误:
The type arguments for method 'System.Web.Mvc.Html.DisplayExtensions.DisplayFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' cannot be inferred from the usage.
无法从用法中推断出方法'System.Web.Mvc.Html.DisplayExtensions.DisplayFor(System.Web.Mvc.HtmlHelper,System.Linq.Expressions.Expression>)'的类型参数。 Try specifying the type arguments explicitly
尝试显式指定类型参数
I've tried it like this: 我已经这样尝试过了:
@Html.ActionLink(@Html.DisplayFor(modelItem => item.LastName), "blah", "blah")
and other variations thereof where it is nested. 及其嵌套的其他变体。 That doesn't work either (won't compile) with the error "no overload method for actionlink.."
错误“操作链接没有重载方法..”也不起作用(不会编译)。
I feel like this should be simple. 我觉得这应该很简单。 Yes, I'm very new to C# and MVC.
是的,我是C#和MVC的新手。
you need to use this overload of Html.ActionLink
: 您需要使用
Html.ActionLink
此重载:
@Html.ActionLink(string LinkText,string ActionName,string ControllerName)
do like this: 这样做:
@Html.ActionLink(Model.LastName, "ActionName","ControllerName")
Updated: 更新:
then do like this, as your Model is IEnumerable: 然后这样做,因为您的模型是IEnumerable的:
@foreach(var item in Model)
{
@Html.ActionLink(item.LastName, "ActionName","ControllerName")
}
If Model is Single like this: 如果模型是这样的:
@model MyCompany.LGCustData
then it should be lke this: 那么应该这样:
@Html.ActionLink(Model.LastName, "ActionName","ControllerName")
@Html.ActionLink(@Model.AccounNumber....)
可以防止有人像我一样出现在这里。
this is a breaf description of how you should use an actionlink 这是您应如何使用动作链接的简短描述
Html.ActionLink("link title",
"ActionMethod",
"ControllerName",
null, // <-- Route arguments.
null // <-- htmlArguments
)
try something like this: 尝试这样的事情:
@Html.DisplayFor(Model.LastName, "actionMethod","controllerName",new{Model.LastName});
*if the case you want to add some parameters to your route, following the standard convention: *如果您要按照标准惯例在路由中添加一些参数,请执行以下操作:
/controller/action/parameter
otherwise 除此以外
@Html.DisplayFor(Model.LastName, "actionMethod","controllerName");
For me I simply convert the "dynamic text" before in a string with String.Format()
function like this 对我来说,我可以像这样简单地使用
String.Format()
函数将“动态文本”转换成字符串
<ul>
@foreach (var user in ViewBag.users )
{
string complete_name = String.Format("{0} {1}", user.Firstname, user.Lastname);
<li>@Html.ActionLink(complete_name, "Details", new { Id = user.Id })</li>
}
</ul>
On my computer it works very fine. 在我的计算机上,它运行良好。
只需使用
@Html.ActionLink(Html.DisplayNameFor(model => model.LastName).ToHtmlString(), "blah", "blah")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.