M-Fulu
M-Fulu

Reputation: 91

Compilation Error when incrementing on a razor View

I am trying to increment on a razor View but "Count" is giving me the below error, I have added @using System.Linq; but I am still getting the same error. Please Assist. Compiler Error Message:

CS1061: 'int' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

for (int i = 0; i < Model.Number.Count(); i++)
{
    var item = Model.Number[i];
    @Html.DisplayFor(model => item.Number, new 
    { 
        htmlAttributes = new { @class = "form-control" } 
    })                            
}


    /// <summary>
    ///  Number
    /// </summary>
    [DisplayName("Number")]
    public int Number { get; set; }

Upvotes: 0

Views: 41

Answers (2)

M-Fulu
M-Fulu

Reputation: 91

Hi thank you everyone I managed to fix it by doing the following. there was no need for a for loop.

   var Number = Model.Number + 1;
   @Html.DisplayFor(model => Number, new { htmlAttributes = new { @class = "form-control" } })

Upvotes: 0

Yiyi You
Yiyi You

Reputation: 18139

Since your model is like:

public class Model
{
    public int Number{get;set;}
}

And you want to display number+1,you can use:

@{Model.Number + 1}

Upvotes: 1

Related Questions