3

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?

1
  • It is something else, at least with a quick test here, the above code works. Commented Dec 6, 2008 at 20:06

3 Answers 3

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?

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.