Lord of Scripts
Lord of Scripts

Reputation: 3619

Razor doesn't recognize List()?

My View begins like this:

@using MyNamespace.Models.Mapping
@{
    PlacemarkKmlModel pmodel = new PlacemarkKmlModel();
    pmodel.Center.Latitude = 8.52115M;
    pmodel.Center.Longitude = -80.35981667M;
    pmodel.KmlObjectTokens kmlIDs = new List<string>();
    pmodel.KmlObjectTokens.Add("Sample1.kml");
    pmodel.KmlObjectTokens.Add("Sample2.kml");
}
@Html.Partial("_Mapping", @pmodel, @ViewData)

And Intellisense is giving me a red underline in the 3rd pmodel statement "The type or namespace 'pmodel' could not be found":

pmodel.KmlObjectTokens kmlIDs = new List<string>();

That is strange because it doesn't give the error in the PlacemarkKmlModel pmodel declaration nor in the use of the same in the statements following that where it even does autocompletion of KmlObjectTokens but it craps out on the 3rd?!.

When I execute the view I get the same error:

CS0246: The type or namespace name 'pmodel' could not be found (are you missing a using directive or an assembly reference?)

The error is misleading in the sense that it complains about pmodel but what it doesn't seem to like is the List().

My model looks like this:

public class PlacemarkKmlModel
{
    public ViewportCenterModel Center { get; set; }

    public List<string> KmlObjectTokens { get; set; }
}

Upvotes: 0

Views: 631

Answers (2)

victorvartan
victorvartan

Reputation: 1002

You can work around the fact that razor treats < string > as an HTML tag like this:

  1. Create a helper class where you have a factory of string lists:

     public class Helpers
     {
        public static List<string> GetNewStringList()
        {
            return new List<string>();
        }
     }
    
  2. Use that factory in the razor view to create a new list of strings:

    @pmodel.KmlObjectTokens = Helpers.GetNewStringList();

But, as Darin said, you should not mix responsibilities in the MVC pattern, so the model should already contain your KmlObjectTokens list populated with the data.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039278

Try like this:

@using MyNamespace.Models.Mapping
@{
    PlacemarkKmlModel pmodel = new PlacemarkKmlModel();
    pmodel.Center.Latitude = 8.52115M;
    pmodel.Center.Longitude = -80.35981667M;
    pmodel.KmlObjectTokens = new List<string>();
    pmodel.KmlObjectTokens.Add("Sample1.kml");
    pmodel.KmlObjectTokens.Add("Sample2.kml");
}
@Html.Partial("_Mapping", pmodel, ViewData)

Notice that in your code you had @( instead of @{ to open the code snippet.

Also notice that the following is invalid C#:

pmodel.KmlObjectTokens kmlIDs = new List<string>();

I guess you wanted to simply assign the KmlObjectTokens property on the pmodel instance in which case the correct syntax is:

pmodel.KmlObjectTokens = new List<string>();

And a final remark : views are not intended to contain C# code and initialize models. That's the responsibility of the controller. I am afraid that you have mixed up the responsibilities in the MVC pattern.

Upvotes: 4

Related Questions