简体   繁体   中英

how can i call a tolist () method in actionresult of controller

How can i call the below list method in a ActionResult of a controller

public StudRes<List<Student>> GetStudent()
{
    Database SQLCon = new Database();
    DataTable dt = new DataTable();

    string query = @"select s.items StudentName from StudentData t1 outer apply dbo.Split(t1.StudentName, ',') s";

    dt = SQLCon.getDatatableFromSQL(query);

    return new LinkRes<List<Student>>()
    {
        result = dt.dtHavingRow(),
        data = (from rw in dt.AsEnumerable()
                select new Student
                {
                    StudentName= rw["StudentName"].ToString(),
                }).ToList()
     };
}

Controller:

public ActionResult StudentCode()
{
    var _Student = new StudRes<List<Student>>();
    var _StudentInfor = new StudentInfor ();
    StudAct _Act = new StudAct();
    _Student = _Act.GetStudent(Cookies.GetCookies("StudentName"));
    _StudentInfor = _Act.GetStudentInfor(Cookies.GetCookies("StudentName"));
    string stud = String.Foramt(_Student.StudentName,_StudentInfor.Email);
}

For "_Student.StudentName" shows error

CS1061: StudRes> does not contain a definition for 'StudentName' and no extension method 'StudentName' accepting a frist argument of type 'StudRes>' could be found (are you missing a using directive or an assembly reference?

I think what you need is to return only 1 student instead of the list. You may want to rework your GetStudent method. Otherwise, as things stand, on the controller, _Student.data.First().StudentName should work. Also, in your GetStudent Method you have a typo SrudentName= rw["StudentName"].ToString() should be StudentName= rw["StudentName"].ToString()

Edit 1.

public class Student
    {
        public string StudentName { get; set; }
        public string StudentEmail { get; set; }
    }

    public StudRes<List<Student>> GetStudents()
    {
        Database SQLCon = new Database();
        DataTable dt = new DataTable();

        string query = @"select s.items StudentName from StudentData t1 outer apply dbo.Split(t1.StudentName, ',') s";

        dt = SQLCon.getDatatableFromSQL(query);

        var results = new StudRes<List<Student>>()
        {
            result = dt.Rows.Count,
            data = (from rw in dt.AsEnumerable()
                    select new Student
                    {
                        StudentName = rw["StudentName"].ToString(),
                    }).ToList()
        };
        foreach (var stu in results.data)
        {
            stu.StudentEmail = GetStudentInfor(stu.StudentName);
        }
        return results;
    }

 public ActionResult StudentCode()
        {
            StudAct _Act = new StudAct();
            var _Student = _Act.GetStudents();

            return View("Name of your view", _Student.ToList());
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM