文本文件字符串拆分然后将每个元素输出到视图表中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文本文件字符串拆分然后将每个元素输出到视图表中相关的知识,希望对你有一定的参考价值。
我目前有一个文本文件,其中包含一个paygroup和一个由管道分隔的创建日期,如下例所示
PG1 | 2019年3月21日 PG2 | 2019年3月21日 PG3 | 2019年3月21日
我试图分割字符串,以便我可以输出这样的东西,但日期分割,并放置在添加日期。
但是,当我提取数据时,它会在一列之下。如何从列表中循环日期以显示在正确的薪资组旁边?我现在将它拆分为读入数组,然后放入列表中。我不确定是否有更好的更优化方式?
View
@model WebApplication2.Models.UploadFiles
@{
ViewBag.Title = "Paygroup Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Update Paygroup</h2>
@using (Html.BeginForm("Edit", "UpdateFiles", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(m => m.PayGroup, new { @class = "control-label" })
@Html.EditorFor(m => m.PayGroup, new { htmlAttributes = new { @class = "form-control", placeholder = Html.DisplayNameFor(m => m.PayGroup) } })
@Html.ValidationMessageFor(m => m.PayGroup, "", new { @class = "text-danger" })
<input type="submit" value="Add" class="btn btn-default" />
<input type="submit" value="Delete" class="btn btn-default" />
</div>
}
<table class="table table-striped">
<tr>
<th>Paygroups</th>
<th>Date added</th>
</tr>
@foreach (var pg in Model.Paygroups)
{
<tr>
<td>@pg</td>
</tr>
}
@foreach (var date in Model.DateAdded)
{
<tr>
<td>@date</td>
</tr>
}
</table>
调节器
public class UpdateFilesController : Controller
{
// GET: Default
public ActionResult Edit()
{
var fullpath = Path.Combine(Server.MapPath("~/sourcefiles"), "paygroup.txt");
List<string> paygroupsList = new List<string>();
List<string> DateList = new List<string>();
string line;
using (var sr = new StreamReader(fullpath))
{
while ((line = sr.ReadLine()) != null)
{
string[] strArray = line.Split('|');
paygroupsList.Add(strArray[0]);
DateList.Add(strArray[1]);
}
UploadFiles model = new UploadFiles()
{
Paygroups = paygroupsList,
DateAdded = DateList
};
return View(model);
}
}
模型
public class UploadFiles
{
public List<string> Paygroups { get; set; }
public List<string> DateAdded { get; set; }
[Required(ErrorMessage = "Please enter a paygroup.")]
[Remote("DoesPaygroupExist", "UpdateFiles", HttpMethod = "POST", ErrorMessage = "Paygroup already exists!")]
public string PayGroup { get; set; }
}
答案
因为您实际上要求视图分别循环每个列。并且您的模型设计不当。
模型应该是这样的:
public class Groups
{
[Required(ErrorMessage = "Please enter a paygroup.")]
[Remote("DoesPaygroupExist", "UpdateFiles", HttpMethod = "POST", ErrorMessage = "Paygroup already exists!")]
public string PayGroup { get; set; }
public List<UploadFiles> files {get; set;}
}
public class UploadFiles
{
public string Paygroup { get; set; }
public string DateAdded { get; set; }
}
你的控制器功能应该填写UploadFiles
列表
public ActionResult Edit()
{
var fullpath = Path.Combine(Server.MapPath("~/sourcefiles"), "paygroup.txt");
List<string> paygroupsList = new List<string>();
List<string> DateList = new List<string>();
string line;
using (var sr = new StreamReader(fullpath))
{
List<UploadFiles> lst = new List<UploadFiles>()
while ((line = sr.ReadLine()) != null)
{
string[] strArray = line.Split('|');
UploadFiles model = new UploadFiles()
{
Paygroups = strArray[0],
DateAdded = strArray[1]
};
lst.Add(model);
}
Groups group = new Groups();
group.files = lst;
return View(group);
}
}
然后在您看来,模型应该是
@model WebApplication2.Models.Groups
<table class="table table-striped">
<tr>
<th>Paygroups</th>
<th>Date added</th>
</tr>
@foreach (var record in Model.files)
{
<tr>
<td>@record.Paygroup </td>
<td>@record.DateAdded </td>
</tr>
}
</table>
以上是关于文本文件字符串拆分然后将每个元素输出到视图表中的主要内容,如果未能解决你的问题,请参考以下文章
将NSArray中的每个NSString元素显示为UIButton Objective C
C ++ - 将文本文件作为字符串读取,然后将字符串拆分为向量
Zapier 中的 Javascript:由“-”拆分为多个输出