拆分文本文件字符串,然后将每个元素输出到视图表中

时间:2019-03-21 17:48:32

标签: c# html asp.net-mvc

我目前有一个文本文件,其中包含一个薪资组和一个创建日期,并用管道将其分隔开,如下例所示。

  

PG1 | 2019/3/21
  PG2 | 3/21/2019
  PG3 | 3/21/2019

我正在尝试分割字符串,以便我可以输出类似这样的内容,但是将日期分割并放置在添加的日期下。

Table

但是,当我提取数据时,它会放在一栏中。如何在列表中循环显示日期,以便在适当的薪资组旁边显示?我目前将它拆分成一个数组,然后放入列表中。我不确定是否有更好的最佳方法?

Completed

控制器

private void MoveMarker(params double[] co_ordinates)
{
    PathAnimationStory.Stop();

    if (co_ordinates.Length == 0)
        return;
    /// the number of co_ordinates must be even
    if (co_ordinates.Length % 2 == 1)
        return;

    Co_ordinates = co_ordinates;
    remaining_nodes = co_ordinates.Length / 2;

    Story_Completed(default, default);
}

private void Story_Completed(object sender, object e)
{
    if(remaining_nodes > 0)
    {
        int next_node = Co_ordinates.Length / 2 - remaining_nodes;
        anim_x.To = Co_ordinates[2 * next_node];
        anim_y.To = Co_ordinates[2 * next_node + 1];
        remaining_nodes--;

        PathAnimationStory.Begin();
    }
}

型号

MoveMarker(100, 0, 100, 100, 0, 100, 100, 0);

1 个答案:

答案 0 :(得分:1)

因为您实际上要求视图分别为每个列循环。并且您的模型设计不正确。

模型应该是这样的:

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>