Alex
Alex

Reputation: 77329

Url type not available in my view?

I see endless examples that involve "Url.Action" in a view to generate a URL dynamically. (See example code below.)

The problem: The type/class "Url" doesn't exist in my views! No IntelliSense, and compiling the site results in a "Type expected" exception. How do I get it in there?

Source Code Snippet (this is from an example):

<form id="register-form" action="<%= Url.Action(new(action="Register")) %>" method="post">
//inputs etc
</form>

I'm using the latest version of MVC.

Here is the entire page (which was generates through "generate view"):

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">Register</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">


<h2>Register</h2>

<form id="register-form" action="<%= Url.Action(new(action="Register")) %>" method="post">
<fieldset>
    <label for="emailAddress"><%= Resources.Labels.EmailAddress %>: </label> <input id="emailAddress" type="text" />
    <label for="password"><%= Resources.Labels.Password %>: </label> <input id="password" type="password" />

    <input id="register-submit" value="<%= Resources.Labels.SubmitRegistration %>" type="submit" />
</fieldset>
</form>

Upvotes: 0

Views: 527

Answers (3)

Alex
Alex

Reputation: 77329

Solved it. The Url.Action(new("Register"))) was the problem. Url.Action("Register") works.

Thanks for your help though. This was hard to tackle because VS underlines the wrong code and gives confusion statements about problems. Glad its solved.

Upvotes: 1

Tomas Aschan
Tomas Aschan

Reputation: 60584

The Url class in the examples is actually a property of the ViewPage, and also of the ViewUserControl, so if your application builds and you have your inheritances correctly set up, this shouldn't be possible.

Try rebuilding your solution a couple of times, and if that not helps restarting Visual Studio. (I used to have the same problem with the Html property, but I believe that was on one of the Preview releases, and I could solve it by upgrading to the next preview/the beta.)

Upvotes: 1

Ropstah
Ropstah

Reputation: 17804

This is probably because the project didn't compile properly. Try to generate the View pages by right-clicking the controller method and click create view... and recompile...

Upvotes: 0

Related Questions