我正在使用一個MVC框架來獲取資料,并在視圖中以某種方式顯示它。
這是我的模型代碼:
這是我的模型代碼。
// Students.cs
使用系統。
using System.Collections.Generic;
namespaceVegam.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.Collections.Generic.Details
using Vegam.Models;
using System.Data.SqlClient;
namespace Vegam.Controllers
{
public class StudentController : Controller
{
//GET: 學生[/span>
public ActionResult Index(Students students)
{
string connection = ConfigurationManager.ConnectionStrings["StudentsConnection"/span>].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"/span>]。
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>/span>
<body>
<center>
<h1>答案 1</h1>
@if(Model != null)
{
如果(Model.studentInfo.Count > 0)
{
<table>
<tr>
<th>學生姓名</th>
<th>EC1</th>/span>
<th>/span>EC2</th>/span>
<th>/span>EC3</th>/span>
<th>/span>EC4</th>/span>
<th>/span>EC5</th>/span>
<th>Total</th>
</tr>
@foreach(var item in Model.studentInfo)
{
<tr>
<td>@Html.DisplayFor(m => item.studentName)</td>/span>
</tr>/span>
}
</table>
}
}
</center> }
</body>/span>
</html>
我不知道如何撰寫其余的<td>元素。不管我寫了什么,都會回傳所有的學生姓名,包括重復的。我應該在視圖中過濾還是在控制器中創建單獨的物件?我不能使用SQL函式或資料表來過濾資料,只能使用自定義物件。
UpDATE
https://stackoverflow.com/users/1410664/caius-jard的建議,這是我的新控制器代碼和視圖代碼:-
public ActionResult Index(Students students)
{
using (var c = new SqlConnection(ConfigurationManager.ConnectionStrings["StudentsConnection"/span>] .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"/span>)
svm.EC1 = s.FMarks;
else if (s.FSubject == "EC2"/span>)
svm.EC2 = s.FMarks;
else if (s.FSubject == "EC3"/span>)
svm.EC3 = s.FMarks;
else if (s.FSubject == "EC4"/span>)
svm.EC4 = s.FMarks;
else if (s.FSubject == "EC5"/span>)
svm.EC5 = s.FMarks;
}
return View("Index", d.Value)。
}
@model Vegam.Models.StudentViewModel
@{
Layout = null;
}
<! DOCTYPE html>
<html>
<head>
< meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>/span>
<body>
<center>
<h1>答案 1</h1>
@if (Model != null)
{
<table>
<tr>/span>
<th>學生姓名</th>
<th>EC1</th>/span>
<th>/span>EC2</th>/span>
<th>/span>EC3</th>/span>
<th>/span>EC4</th>/span>
<th>/span>EC5</th>/span>
<th>Total</th>
</tr>/span>
<tr>/span>
<td>/span>@Model.Name</td>/span>
</tr>/span>
</table>
}
</center>
</body>/span>
</html>
uj5u.com熱心網友回復:
在資料庫中完成,你的資料透視看起來像:
SELECT FStudentName, ec1, ec2, ec3, ec4, ec5, ec1 ec2 ec3 ec4 ec5 as Tot,
FROM student
PIVOT(
SUM(FMarks)
FOR FSubject IN (ec1, ec2, ec3, ec4, ec5)
) x
用C#來做,它可能看起來像:
using var c = new SqlConnection("conn str here"/span>) 。
//使用Dapper;生命太短暫了,不能用它來寫麻木的東西。
//while reader.read someobject.someproperty = reader.getxxx("somecolumn")
//adjust your Students class so it is called Student
//具有與資料庫列名稱相匹配的屬性。
var ss = c.Query<Student>("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"/span>)
svm.EC1 = s.FMarks;
else if(s.FSubject == "EC2"/span>)
svm.EC2 = s.FMarks;
else if(s.FSubject == "EC3"/span>)
svm.EC3 = s.FMarks;
else if(s.FSubject == "EC4"/span>)
svm.EC4 = s.FMarks;
else if(s.FSubject == "EC5"/span>)
svm.EC5 = s.FMarks;
}
return View("Index", d.Values) 。
你需要一個類StudentViewModel,有一個名字和5個屬性EC1到EC5。大多數情況下,Visual Studio會以你的形式來寫;只要指向每一行,并接受相關的建議,如 "生成StudentViewModel類"、"添加成員...創建屬性EC1 "等。它將從它對學生類的了解中獲得所有的型別。學生類是資料庫方面的東西,你的視圖不應該使用它。學生的屬性應該映射到資料庫的列名上,這讓生活變得更簡單;安裝Dapper(右擊專案參考,管理nuget包,添加Dapper。Dapper負責所有資料庫的查詢,讀取結果并為你填充一個學生實體
我知道你說你想在 c# 中進行透視,但我把 db 版本放在那里,主要是為了強調在 db 端進行透視是多么容易......這也導致了從 db 到服務器的網路資料量大大減少。當你撰寫繁忙的應用程式時,你必須盡可能地進行優化。有很多其他的自然方法來透視資料,但主要是我的目標是遠離LINQ和過于復雜的資料結構,并給出一些你希望可以遵循的方法。資料透視的主要思想是你有一個重復值的串列(學生ID)。當你第一次遇到它時,它不在你的字典里,所以 TryGetValue 回傳 false。這是創建跟蹤屬性的StudentViewModel的提示,并將其添加到字典中,這樣下次我們看到相同的資料時,如果我們將新的資料(不同的分數/科目)放入已經創建的實體中。這樣一來,資料的順序就不重要了;在結果集結束時,你會有一個唯一的學生ID的字典,每個StudentViewModel最多可以填入5個屬性。你從資料庫中得到的10行結果集已經變成了有5個屬性的2個物件。你的視圖所要做的就是把td的5個線,每個Mark一個,你就可以分別訪問每個不同的ECx。有一件事C#可以做到,但在資料庫中卻不那么容易;樞軸中的動態列數。為此,你可以將你的標記存盤在一個字典中,將主題名稱映射到標記上。你可以創建一個所有主題名稱的超集,然后在布置你的視圖時,對每一個主題進行TryGetValue,如果有標記的話,就把它拿出來。這要復雜得多;在進入動態透視之前,也許可以先了解一下這里的簡單版本
。轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/332320.html
標籤:
下一篇:Python-多重處理多個檔案夾

