【发布时间】:2021-09-21 04:16:32
【问题描述】:
我在 SQL Server 中有一个学生详细信息表,如下所示:
我正在使用 MVC 框架来获取数据并在视图中以某种方式显示它。
这是我的模型代码:
// Students.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Vegam.Models
{
public class Students
{
public int studentId { get; set; }
public string studentName { get; set; }
public string subject { get; set; }
public int marks { get; set; }
public List<Students> studentInfo { get; set; }
}
}
我在控制器中创建了一个对象来访问数据:
// StudentController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vegam.Models;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Vegam.Controllers
{
public class StudentController : Controller
{
// GET: Student
public ActionResult Index(Students students)
{
string connection = ConfigurationManager.ConnectionStrings["StudentsConnection"].ConnectionString;
SqlConnection sqlConnection = new SqlConnection(connection);
string query = "select * from [dbo].[Student]";
SqlCommand sqlCommand = new SqlCommand(query);
sqlCommand.Connection = sqlConnection;
sqlConnection.Open();
SqlDataReader sdr = sqlCommand.ExecuteReader();
List<Students> studentsModel = new List<Students>();
if(sdr.HasRows)
{
while(sdr.Read())
{
var studentDetails = new Students();
studentDetails.studentId = (int) sdr["FStudentID"];
studentDetails.studentName = sdr["FStudentName"].ToString();
studentDetails.subject = sdr["FSubject"].ToString();
studentDetails.marks = (int) sdr["FMarks"];
studentsModel.Add(studentDetails);
}
students.studentInfo = studentsModel;
sqlConnection.Close();
}
return View("Index", students);
}
}
}
在我看来,我想以这种表格格式返回数据:
到目前为止,我已经想出了这个代码:
// Index.cshtml
@model Vegam.Models.Students
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<center>
<h1>Answer 1</h1>
@if(Model != null)
{
if(Model.studentInfo.Count > 0)
{
<table>
<tr>
<th>Student Name</th>
<th>EC1</th>
<th>EC2</th>
<th>EC3</th>
<th>EC4</th>
<th>EC5</th>
<th>Total</th>
</tr>
@foreach(var item in Model.studentInfo)
{
<tr>
<td>@Html.DisplayFor(m => item.studentName)</td>
</tr>
}
</table>
}
}
</center>
</body>
</html>
我不知道如何编写其余的<td> 元素。无论我写什么,都会返回所有学生姓名,包括重复的姓名。我应该在视图中过滤还是在控制器中创建单独的对象?我不能使用 SQL 函数或数据表来过滤数据,只能使用自定义对象。
更新
我接受了https://stackoverflow.com/users/1410664/caius-jard 的建议,这是我的新控制器代码和视图代码:-
public ActionResult Index(Students students)
{
using (var c = new SqlConnection(ConfigurationManager.ConnectionStrings["StudentsConnection"].ConnectionString)) {
var ss = c.Query<Students>("select * from student");
var d = new Dictionary<int, StudentViewModel>();
foreach (var s in ss)
{
StudentViewModel svm;
if (!d.TryGetValue(s.FStudentID, out svm)){
d[s.FStudentID] = svm = new StudentViewModel();
svm.Name = s.FStudentName;
}
if (s.FSubject == "EC1")
svm.EC1 = s.FMarks;
else if (s.FSubject == "EC2")
svm.EC2 = s.FMarks;
else if (s.FSubject == "EC3")
svm.EC3 = s.FMarks;
else if (s.FSubject == "EC4")
svm.EC4 = s.FMarks;
else if (s.FSubject == "EC5")
svm.EC5 = s.FMarks;
}
return View("Index", d.Values);
}
}
@model Vegam.Models.StudentViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<center>
<h1>Answer 1</h1>
@if (Model != null)
{
<table>
<tr>
<th>Student Name</th>
<th>EC1</th>
<th>EC2</th>
<th>EC3</th>
<th>EC4</th>
<th>EC5</th>
<th>Total</th>
</tr>
<tr>
<td>@Model.Name</td>
</tr>
</table>
}
</center>
</body>
</html>
【问题讨论】:
-
根据问题指南,请不要发布代码、数据、错误消息等的图像 - 将文本复制或输入到问题中。请保留将图像用于图表或演示渲染错误,无法通过文本准确描述的事情。
-
我不能使用 SQL 函数 - 如果你要做的只是
select *大量数据它并在 c# 中处理它。数据库在处理数据方面比 c# 好得多,并且要执行客户端数据透视,正如您所发现的,必须将相对大量的数据从 db 传输到 c#。你的基本问题是你需要做一个支点,而你没有。您可以在数据库中执行此操作并节省大量资源并使您的 c# 生活更轻松。您可以在 c# 控制器中执行此操作并保持视图简单,或者您可以在视图中执行此操作.. -
ps;对公共属性使用 PascalCase,而不是 camelCase。不要给类提供复数名称。如果一个类代表一个集合,则在它后面加上 Collection。如果一个属性返回一个集合,给它一个复数名称
-
对于一个学生实例也有一个学生列表在这种情况下并没有什么意义;它似乎仅存在,因此您可以将学生列表返回到视图,并且您让运行时通过将其作为 get 的参数来创建持有学生,这有点奇怪。只需让您的索引创建一个 List
并返回它,从学生类中删除 List 属性
标签: c# sql-server asp.net-mvc