VB.NET & ASP.NET MVC导入CSV文件并在表格显示数据问题
解决CSV数据无法在表格正确显示的问题
问题根源
- 数据存储错误:你在Controller中使用的
TSetList是单个字符串数组,循环读取CSV每行数据时会反复覆盖数组内容,最终ViewData("transactions")仅保留了CSV的最后一行数据。 - 视图渲染错误:直接用
@Html.Raw(ViewData("transactions"))输出数组,ASP.NET MVC会直接输出数组的类型字符串(如System.String[]),不会自动生成表格行结构,导致表格无法正常展示数据。
解决方案步骤
1. 创建交易数据模型类
先定义一个类来封装每行交易数据,让数据结构更清晰:
Public Class Transaction Public Property SortCode As String Public Property AccountNumber As String Public Property AccountName As String Public Property Reference As String Public Property RTI As String Public Property Value As String End Class
2. 修改Controller代码
将单个字符串数组替换为List(Of Transaction),循环读取每行数据时添加到列表中,最后把整个列表传入ViewData:
Function Create() As ActionResult Return View() End Function <HttpPost()> Function Create(upload As HttpPostedFileBase) As ActionResult Try If upload IsNot Nothing Then Dim _skiplines As Integer = 3 Dim _posSortcode As Integer = 0 Dim _posAccountNumber As Integer = 1 Dim _posAccountName As Integer = 2 Dim _posReference As Integer = 4 Dim _posRTI As Integer = 999 Dim _posValue As Integer = 3 ' 初始化存储所有交易数据的列表 Dim transactions As New List(Of Transaction) If upload.ContentLength > 0 Then Using reader = New System.IO.StreamReader(upload.InputStream) ' 跳过指定行数 If _skiplines > 0 Then For Counter = 1 To _skiplines reader.ReadLine() Next End If ' 循环读取每行数据 Do While Not reader.EndOfStream Dim LineArray = Split(reader.ReadLine, ",") Dim trans As New Transaction ' 为当前交易对象赋值 trans.SortCode = If(_posSortcode <> 999, LineArray(_posSortcode), "") trans.AccountNumber = If(_posAccountNumber <> 999, LineArray(_posAccountNumber), "") trans.AccountName = If(_posAccountName <> 999, LineArray(_posAccountName), "") trans.Reference = If(_posReference <> 999, LineArray(_posReference), "") trans.RTI = If(_posRTI <> 999, LineArray(_posRTI), "") trans.Value = If(_posValue <> 999, LineArray(_posValue), "") ' 将当前交易添加到列表 transactions.Add(trans) Loop ViewData("transactions") = transactions reader.Close() End Using ViewBag.Message = "Completed With " & transactions.Count.ToString() End If Else ViewBag.Message = "No File Found" End If Catch ex As Exception ViewBag.Error = ex.Message.ToString End Try Return View() End Function
3. 修改View代码
在视图中遍历ViewData里的交易列表,动态生成表格行:
@Using (Html.BeginForm("Create", "File", Nothing, FormMethod.Post, New With {.enctype = "multipart/form-data"})) @<div class="form-horizontal"> <h4>Instructor</h4> <hr /> <div class="form-group"> <div class="col-md-10"> <input type="file" id="ImportFile" name="upload" /> </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> @ViewBag.Message @If ViewBag.Error IsNot Nothing Then @<div class="alert alert-danger">@ViewBag.Error</div> End If End Using <table class="table"> <thead> <tr> <th>Sort Code</th> <th>Account Number</th> <th>Account Name</th> <th>Reference</th> <th>RTI</th> <th>Value</th> </tr> </thead> <tbody id="helpRowBody"> @If ViewData("transactions") IsNot Nothing Then Dim transactions = CType(ViewData("transactions"), List(Of Transaction)) For Each trans In transactions @<tr> <td>@trans.SortCode</td> <td>@trans.AccountNumber</td> <td>@trans.AccountName</td> <td>@trans.Reference</td> <td>@trans.RTI</td> <td>@trans.Value</td> </tr> Next End If </tbody> </table>
内容的提问来源于stack exchange,提问作者PeteN
相关产品推荐
相关产品推荐

