I'm developing an MVC application, see below:
RecordRep.text
000185000 | 3 | 13072016 | 2357 | 12942973988 000185001 | 3 | 14072016 | 0000 | 12739666685 000185002 | 3 | 14072016 | 0800 | 12519046076 000185003 | 3 | 14072016 | 0800 | 13303022983 000185004 | 3 | 14072016 | 0800 | 16140197229 000185005 | 3 | 14072016 | 0800 | 13136849980 000185006 | 3 | 14072016 | 0801 | 11988133208
namespace WebAbrirArquivo.Models
{
public class AbrirArquivo
{
public string MeuPis { get; set; }
}
}
//---------------
namespace WebAbrirArquivo.Controllers
{
public class AbrirArquivoController : Controller
{
public string Conteudo { get; private set; }
public string StrLinha { get; private set; }
public string Nsr { get; private set; }
public string Dtop { get; private set; }
public string HoraOp { get; private set; }
public string NPis { get; private set; }
// GET: AbrirArquivo
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
{
IList<string> linesArq = new List<string>();
IList<String> BuscaQuadro = new List<String>();
var outputFile = System.IO.File.CreateText(Server.MapPath(@"~/App_Data/Uploads/OutputFile.txt"));
foreach (var file in files)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
using (StreamReader reader = new StreamReader(file.FileName))
{
while (!reader.EndOfStream)
{
StrLinha = reader.ReadLine();
Nsr = StrLinha.Substring(0, 9);
HoraOp = StrLinha.Substring(21, 4);
NPis = StrLinha.Substring(26, 11);
var DtOpC = StrLinha.Substring(12, 2) + "/" + StrLinha.Substring(14, 2) + "/" + StrLinha.Substring(16, 4) +
" " + StrLinha.Substring(21, 2) + ":" + StrLinha.Substring(23, 2) + ":" + 0 + 0;
linesArq.Add(Nsr + " " + NPis + " " + DtOpC + "\r\n");
outputFile.WriteLine(Nsr + " " + NPis + " " + DtOpC);
}
}
// IEnumerable<string> GUARDACOL = linesArq.Where(Nsr => Nsr.Contains("12519046076")); // Valor passado diretamente funciona OK
IEnumerable<string> GUARDACOL = linesArq.Where(Nsr => Nsr.Contains(AbrirArquivo.MeuPis)); // Valor da View ERRO:
foreach (string NSR in GUARDACOL)
{
BuscaQuadro.Add(NSR);
}
// create new txt file
var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), fileName);
file.SaveAs(path);
}
}
ViewBag.linesArq = BuscaQuadro;
// return RedirectToAction("Upload");
//if (files.Count() > 0) Console.WriteLine(files.Count()); // display 1
//if (files.Any()) Console.WriteLine(files.Any()); // display true
// if (files.First() == null) Console.WriteLine("first null"); // display "first null"
return View();
}
// GET: OpenUpload
public ActionResult Upload()
{
return View();
}
}
}
//------------
@model WebAbrirArquivo.Models.AbrirArquivo
@{
ViewBag.Title = "Index";
}
<h2>Localizar Arquivo</h2>
@using (Html.BeginForm("Upload", "AbrirArquivo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
@Html.LabelFor(model => model.MeuPis, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.MeuPis, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.MeuPis)
</div>
</div>
<p></p> <input name="files" type="file" multiple="multiple" />
<input type="submit" value="Upload" />
}
<script src="@Url.Content("~/Scripts/methods_pt.js")" type="text/javascript"></script>
//--------------------
@{
ViewBag.Title = "Upload";
}
<h2>Arquivo Uploaded</h2>
<ul>
@foreach (string linesArq in ViewBag.linesArq)
{
<li>
@linesArq
</li>
}
</ul>
ERROR: An object reference would be required for the field "IEnumerable GUARDACOL = linesArq.Where (Nsr => Nsr.Contains (OpenFile.MyPis))".