如何在ASP.Net MVC中进行JavaScript重定向?

时间:2022-10-30 09:23:17

The following code will generate a link to the page I want to get to.

以下代码将生成指向我想要访问的页面的链接。

<%= Html.ActionLink(image.Title, image.Id.ToString(), "Image") %>

The following code will cause the correct url to be rendered on the page.

以下代码将导致在页面上呈现正确的URL。

<%= Url.Action("Index", "Image", new { id = image.Id })%>

But when I try to use it in javascript it fails. (with some strange error about page inheritance)

但是当我尝试在javascript中使用它时失败了。 (有关页面继承的一些奇怪错误)

<div onclick="window.location = '<%= Url.Action("Index", "Image", new { id = image.Id })%>'">
    ...
</div>

Should the above code work? What is the correct way to generate the javascript attempted above?

上述代码应该有效吗?生成上面尝试的javascript的正确方法是什么?

Update There error I get is

更新我得到的错误是

Views\Home\Index.aspx.cs(9): error ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).

Views \ Home \ Index.aspx.cs(9):错误ASPNET:确保此代码文件中定义的类与'inherits'属性匹配,并且它扩展了正确的基类(例如Page或UserControl)。

Looks like it indicates a bigger problem.

看起来它表明一个更大的问题。

Fixed Thanks for your help, the code contained a div with runat="server". When I removed this it runs OK. This maybe because there is no form with runat="server" but I would expect a different error for that.

修复感谢您的帮助,代码中包含一个带有runat =“server”的div。当我删除它时它运行正常。这可能是因为没有runat =“server”的形式,但我希望有一个不同的错误。

As this question doesn't seem meaningful should I delete it?

因为这个问题似乎没有意义我应该删除吗?

3 个解决方案

#1


2  

This should work actually. ASP.NET MVC will substitute all <%= ... %> or similar tags, it does not recognize whether it's a html definition or javascript. What is the output of your view? Is the "strange error" coming from Javascript or ASP.NET?

这实际上应该有效。 ASP.NET MVC将替换所有<%= ...%>或类似标签,它无法识别它是html定义还是javascript。您的视图输出是什么?来自Javascript或ASP.NET的“奇怪错误”是什么?

EDIT: regarding your update: make sure your Index.aspx has the "Codebehind" attribute (this is in Page-tag on the very first line) pointing to Index.aspx.cs and the attribute "Inherits" contains the class name of the Page/User-Control class in the code-behind.

编辑:关于您的更新:确保您的Index.aspx具有指向Index.aspx.cs的“Codebehind”属性(这在第一行的Page-tag中),属性“Inherits”包含类的名称代码隐藏中的Page / User-Control类。

#2


1  

Take a look at this for a possible solution to your error message. Codebehind vs Codefile.

请查看此错误消息的可能解决方案。 Codebehind与Codefile。

#3


0  

It looks like you have a HomeController with an Image method correct? Then it should be

看起来你有一个HomeController与Image方法正确吗?那应该是

<%= Url.Action("Image", "Home", new { id = image.Id })%>

#1


2  

This should work actually. ASP.NET MVC will substitute all <%= ... %> or similar tags, it does not recognize whether it's a html definition or javascript. What is the output of your view? Is the "strange error" coming from Javascript or ASP.NET?

这实际上应该有效。 ASP.NET MVC将替换所有<%= ...%>或类似标签,它无法识别它是html定义还是javascript。您的视图输出是什么?来自Javascript或ASP.NET的“奇怪错误”是什么?

EDIT: regarding your update: make sure your Index.aspx has the "Codebehind" attribute (this is in Page-tag on the very first line) pointing to Index.aspx.cs and the attribute "Inherits" contains the class name of the Page/User-Control class in the code-behind.

编辑:关于您的更新:确保您的Index.aspx具有指向Index.aspx.cs的“Codebehind”属性(这在第一行的Page-tag中),属性“Inherits”包含类的名称代码隐藏中的Page / User-Control类。

#2


1  

Take a look at this for a possible solution to your error message. Codebehind vs Codefile.

请查看此错误消息的可能解决方案。 Codebehind与Codefile。

#3


0  

It looks like you have a HomeController with an Image method correct? Then it should be

看起来你有一个HomeController与Image方法正确吗?那应该是

<%= Url.Action("Image", "Home", new { id = image.Id })%>