Reputation: 56906
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.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)
<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?
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).
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.
As this question doesn't seem meaningful should I delete it?
Upvotes: 3
Views: 7964
Reputation: 17282
It looks like you have a HomeController with an Image method correct? Then it should be
<%= Url.Action("Image", "Home", new { id = image.Id })%>
Upvotes: 0
Reputation: 676
Take a look at this for a possible solution to your error message. Codebehind vs Codefile.
Upvotes: 1
Reputation: 11358
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?
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.
Upvotes: 2