I cannot use List<string> as Model in Razor

Cerberus 21 Reputation points
2022-01-09T19:50:09.057+00:00

Hi Guys I tried to use Razor but I get this error really couldn't get it

List<string> products = new List<string>  
    {  
        "Ruler",  
        "Eraser",  
        "Pen",  
        "Pencil",  
        "Notebook",  
        "Book"  
    };  
    public IActionResult Products()  
    {  
          
        return View(products);  
    }   

163413-untitled.jpg

Developer technologies ASP.NET ASP.NET Core
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Rebin Qadir 151 Reputation points Volunteer Moderator
    2022-01-10T09:32:07.617+00:00

    Hi,

    This should be working in VS code just clean you solution and build it again

    In the VS Code terminal just run those commands

    1. dotnet clean
    2. dotnet build

    Also, you can disable the null refence compiler warning in .csproj file just replace <Nullable>enable</Nullable> to <Nullable>disable</Nullable>

     public class HomeController : Controller
        {
    
            private readonly List<string> _poducts;
    
            public HomeController(ILogger<HomeController> logger)
            {
    
    
    
        _poducts = new List<string>
            {
             "Ruler",
             "Eraser",
             "Pen",
             "Pencil",
             "Notebook",
             "Book"
            };
    
            }
    
            public IActionResult Index()
            {
                return View(_poducts);
            }
    
    
        }
    

    The razor page

    @model List<string>
    
    <div class="text-center">
    
        @foreach(var item in Model)
        {
    
           <h1>@item</h1>
    
    
        }
    
    
    
    </div>
    

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2022-01-10T02:16:51.32+00:00

    Hi @Cerberus ,

    According to your warning message, it means the compile find the model may throw the null reference error when the project running. You could ignore it or check the model if this is a null reference error or not. More details, you could refer to this article.

    But for the below error, it says the product.cshtml contains some not existed variable, I suggest you could reopen the product.cshtml to make sure you have save the modified content and rebuild again.

    1 person found this answer helpful.

  2. Rebin Qadir 151 Reputation points Volunteer Moderator
    2022-01-09T20:58:04.617+00:00

    1-create a Model class to holds the products like this

        public class Products
        { 
            public string? Name { get; set; }
        }
    

    2- In the controller constructor you can initialize the list of the products.

     private readonly List<Products> _listProducts;
    
            public HomeController()
            {
    
                _listProducts = new List<Products>() 
    
                {   new Products { Name= "Ruler" }, 
                    new Products { Name = "Eraser" }, 
                    new Products { Name = "Pen" }, 
                    new Products { Name = "Pencil" }, 
                    new Products { Name = "Notebook" },
                    new Products { Name = "Book" } 
                };
    
            }
    

    3- In the IActionResult method pass the product list to the view.

           public IActionResult Index()
            {
                return View(_listProducts);
            }
    

    4- In the view page

    @model IEnumerable<Products>
    
    
    <div class="text-center">
    
        @foreach(var item in Model)
        {
            <h3>@item.Name</h3>
        }
    
    
    
    </div>
    

    I hope this helps you

    0 comments No comments

  3. Cerberus 21 Reputation points
    2022-01-09T21:20:13.433+00:00

    Thanks a lot bro why mine is not working cause tutorial I have watched that was 3 or 4 version of Asp.NET core well why it didnt work in Asp,Net core 6?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.