2

Has anyone had experience with Razor without MVC. I am using a Template engine build up from http://www.fidelitydesign.net/?p=208 but I cannot use any of the linq queries within the Razor code for example

<div>
    @Model.Person.First().Firstname
</div>

This throws the error:

System.Collections.Generic.List' does not contain a definition for 'First' and no extension method 'First' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference

For this example Model contains a List of Person below

public class Person
{
    public string FirstName { get; set; }
}
4
  • 2
    I assume you did try @using System.Linq ? Commented Mar 13, 2012 at 13:09
  • Yes, I have debugged and checked that the assembley was added Commented Mar 13, 2012 at 13:13
  • the syntax is wrong, Model is the list, so First needs to be called on Model. Commented Mar 13, 2012 at 13:13
  • @BrokenGlass: As a side note to your addition: If you use Linq in more of your views than it is easier to put it in the Web.Config file under <system.web><compilation><assemblies><add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> Don't forget to restart Visual Studio :) Commented Mar 13, 2012 at 13:25

1 Answer 1

1

The Model is the list, so you need to First on the model not Person, First returns an instance of the Person class:

<div>
    @Model.First().Firstname
</div>

You need to :

  • add @using System.Data.Linq at the top of your view
  • or if you need it on all views add <add namespace="System.Data.Linq" /> to the web.config in your Views folder.

Here is how you do it:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
            <add namespace="System.Data.Linq" />
        </namespaces>
    </pages>
</system.web.webPages.razor>
Sign up to request clarification or add additional context in comments.

6 Comments

I have updated the question to say that 'Model' contains a List of 'Person'. There is a far deeper issue than this. Thanks though.
Don't forget to restart Visual Studio for Intellisense support on Linq.
There is no Views folder, this is a "Class library" project and the html markup comes from the DB, please check the link for an example project
So put the section I provided into the app.config of the application that is actually running this code OR put it on top of the view
I should have mentioned that I did try this before my last comment with no luck.
|

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.