c# - 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());
}
推荐阅读
- javascript - 灯塔作为 RESTful api
- jquery - 在 dragend 的弹出窗口中获取地址
- html - 应用溢出隐藏时删除小边框
- gradle - 无法独立导入 grails 插件:9.0.0.M4
- logging - kubernetes:kubectl 只流式传输一定数量的日志
- macos - 在 macOS 10.8 (Mountain Lion) 之后已弃用的资源 'CurResfile'、'UseResFile' 等的替代 API
- c# - 如何创建一个行数并不总是相同的滚动列表?
- c# - 为什么编译器与继承中的命名空间冲突
- scala - 如何从配置文件在数据框中添加架构
- r - 使用 R 生成用于情感分析的表格和图表