我目前有一个文本文件,其中包含一个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";
}
Update Paygroup
@using (Html.BeginForm("Edit", "UpdateFiles", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@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" })
}
Paygroups | Date added |
---|
@foreach (var pg in Model.Paygroups)
{
@pg}
@foreach (var date in Model.DateAdded)
{
@date}
调节器
public class UpdateFilesController : Controller
{
// GET: Default
public ActionResult Edit()
{
var fullpath = Path.Combine(Server.MapPath("~/sourcefiles"), "paygroup.txt");
List paygroupsList = new List();
List DateList = new List();
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 Paygroups { get; set; }
public List DateAdded { get; set; }
[Required(ErrorMessage = "Please enter a paygroup.")]
[Remote("DoesPaygroupExist", "UpdateFiles", HttpMethod = "POST", ErrorMessage = "Paygroup already exists!")]
public string PayGroup { get; set; }
}