我在razor 3和mvc 5下编码。
以下是我在视图中写的foreach
:
@if (TempData["ImageNames"] != null)
{
List<string> fileNames = (List<string>)TempData["ImageNames"];
foreach (var fileName in fileNames.ToList())
{
<p>@fileName</p>
}
}
列表不是空的,但没有打印。
可能出现什么问题?
更新
以下是完整的Html部分:
<div class="col-lg-6">
<div class="panel panel-primary" style="min-height: 215px;">
<div class="panel-heading">Upload File</div>
<div class="panel-body">
<form action="/document/upload" class="dropzone" id="my-awesome-dropzone" enctype="multipart/form-data" method="post">
</form>
<hr />
@{List<string> fileNames = (List<string>)TempData["ImageNames"];}
@if (TempData["ImageNames"] != null)
{
foreach (var fileName in fileNames.ToList())
{
<p>@fileName</p>
}
}
</div>
</div>
</div>
另一次更新
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
string fileName = Guid.NewGuid().ToString() + ".pdf";
string path = Path.Combine(Server.MapPath("~/Files/Pdf/"), fileName);
file.SaveAs(path);
PdfReader reader = null;
iTextSharp.text.Document document = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
List<string> pdfNames = new List<string>();
List<string> imageNames = new List<string>();
try
{
reader = new PdfReader(path);
for (int pageIndex = 1; pageIndex <= reader.NumberOfPages; pageIndex++)
{
string guid = Guid.NewGuid().ToString();
string fileName1 = guid+ ".pdf";
string fileName2 = guid + ".png";
pdfNames.Add(fileName1);
imageNames.Add(fileName2);
string path1 = Path.Combine(Server.MapPath("~/Files/Pdf/temp/"), fileName1);
document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(pageIndex));
pdfCopyProvider = new PdfCopy(document,new System.IO.FileStream(path1, System.IO.FileMode.Create));
document.Open();
importedPage = pdfCopyProvider.GetImportedPage(reader, pageIndex);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(importedPage);
pdfCopyProvider.AddPage(importedPage);
document.Close();
PDFDocument pdfDoc = new PDFDocument();
pdfDoc.LoadPDF(path1);
Bitmap pngImage = pdfDoc.ToImage(0);
string path2 = Path.Combine(Server.MapPath("~/Images/Pdf/temp/"), fileName2);
pngImage.Save(path2, ImageFormat.Png);
}
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
TempData["ImageNames"] = imageNames;
return RedirectToAction("index");
}
答案 0 :(得分:0)
可能是我的语法会有点错误,因为我无法在这里打开visual studio,但据我所知,为了显示UI中的变量,你需要put&#34;:&#34;在你的变量之前。所以代码将是这样的:
@if (TempData["ImageNames"] != null)
{
List<string> fileNames = (List<string>)TempData["ImageNames"];
foreach (var fileName in fileNames.ToList())
{
<p>@:fileName</p>
}
}
答案 1 :(得分:0)
幽默我,试着去做:
@{
if (TempData["ImageNames"] != null) {
List<string> fileNames = (List<string>)TempData["ImageNames"];
foreach (var fileName in fileNames)
{
<p>@Html.Raw(fileName)</p>
}
}
}
答案 2 :(得分:0)
<text>@fileName</text>
,请使用此功能,我希望它能正常工作。