Access ViewData in a Razor view

0

I'm making a list of students to show it in a Razor view using ViewData.

controller

using ViewData.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ViewBag.Controllers
{
    public class StudentController : Controller
    {
        // GET: Student        
        public ActionResult Index()
        {
            IList<Student> studentList = new List<Student>();
            studentList.Add(new Student() { StudentName = "Bill" });
            studentList.Add(new Student() { StudentName = "Steve" });
            studentList.Add(new Student() { StudentName = "Ram" });

            ViewData["students"] = studentList;
            return View();
        }
    }
}

Student class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ViewData.Models
{
    public class Student
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public int Age { get; set; }
    }
}

view

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>students</title>
</head>
<body>
    <div>
        <ul>
            @foreach (var std in ViewData["students"] as IList<Student>)
            {
                <li>
                    @std.StudentName
                </li>
            }
        </ul>

    </div>
</body>
</html>

in the view I want to go through the view but it tells me that in @foreach (var std in ViewData["students"] as IList<Student>) Student is not recognizing me. What can it be?

    
asked by Andrés Oporto 05.05.2017 в 00:52
source

2 answers

1

The problem is that you do not have a reference to the namespace of the Student class.

You have two options:

  • Add a using directive in the view:

    @using ViewData.Models

  • Add the namespace ViewData.Models in the web.config as namespace to include in the Razor views:

    <system.web.webPages.razor>
      <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>
          .....
          <add namespace="MVC_CSharp.Models"/>
        </namespaces>
      </pages>
    </system.web.webPages.razor>
    
answered by 05.05.2017 / 01:10
source
1

It is not necessary to use the ViewData , in the view you must refer to the model you are using, just return the list you generate of students using return View(studentList); :

Code of Controller :

public ActionResult Index()
{
    List<Student> studentList = new List<Student>();
    studentList.Add(new Student() { StudentName = "Bill" });
    studentList.Add(new Student() { StudentName = "Steve" });
    studentList.Add(new Student() { StudentName = "Ram" });

    return View(studentList);
}

Code of View :

@model IEnumerable<Student>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>students</title>
</head>
<body>
    <div>
        <ul>
            @foreach (var item in Model)
            {
                <li>
                    @Html.DisplayFor(modelItem => item.StudentName)
                </li>
            }
        </ul>
    </div>
</body>
</html>

Note that at the beginning of the View you will be using as a model a collection of the class Student , just with @model IEnumerable<Student> , in this way, since you already have a IEnumerable you can already go directly with the foreach .

    
answered by 05.05.2017 в 01:10