从模型属性在 ASP.NET MVC 2 中创建外部链接
我正在尝试使用视图页面中模型类的某些属性来构建到外部网站的链接,例如我希望它呈现为类似 -
< a href="http://twitter.com/home?status=当前正在阅读 http://www. mywebsite.com/post-id">Twitter < /a>
其中 post-id 将从模型中提取(我在这里使用强类型视图模型 - 我不能使用 ViewData,因为有一个项目列表被拉回,所有这些项目都需要单独的链接)。
我尝试做< a href="<% model.PermaLink(); %>">,其中 PermaLink 只是一些字符串,但不会获取该值。
如果我尝试< a href="<%= model.PermaLink %>">,我收到错误 - CS1502: 'System.IO.TextWriter.Write(char)' 的最佳重载方法匹配有一些无效参数
是否有方法来做到这一点?我尝试添加 runat="server" 属性,但这只是链接到我的本地站点和控制器/模型操作。
I'm trying to build a link to an external website using some properties of my model class in my view page, e.g. I want it to render as something like -
< a href="http://twitter.com/home?status=Currently reading http://www.mywebsite.com/post-id">Twitter < /a>
where post-id would be pulled from the model (I'm using a strongly typed view model here - I can't use ViewData as there's a list of items being pulled back all of which need seperate links).
I tried doing < a href="<% model.PermaLink(); %>">, where PermaLink is just some string, however the value doesn't get picked up.
If I try < a href="<%= model.PermaLink %>">, I get an error - CS1502: The best overloaded method match for 'System.IO.TextWriter.Write(char)' has some invalid arguments
Is there any way to do this? I tried adding the runat="server" attribute, however this just linked to my local site and controller/model actions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是基于 ViewPage 类的 MVC ViewPage,还是 WebForms 页面?
<代码>< href="<%= model.PermaLink %>"> 语法对于 MVC 页面是正确的
Is this within an MVC ViewPage, based on the ViewPage class, or is it a WebForms page?
the
< a href="<%= model.PermaLink %>">
syntax is correct for an MVC Page哎呀,没有正确设置我的属性,只是使用常规吸气剂。也许我应该回到 Java :)
Whoops, hadn't setup my property properly, was just using a regular getter. Maybe I should go back to Java :)