Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的剃须刀怎么了?_C#_Asp.net Mvc_Razor - Fatal编程技术网

C# 我的剃须刀怎么了?

C# 我的剃须刀怎么了?,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我在Razor3和MVC5下编码 下面是我在视图中编写的一个foreach: @if (TempData["ImageNames"] != null) { List<string> fileNames = (List<string>)TempData["ImageNames"]; foreach (var fileName in fileNames.ToList()) { <p>@fileName</p>

我在Razor3和MVC5下编码

下面是我在视图中编写的一个
foreach

@if (TempData["ImageNames"] != null)
{
    List<string> fileNames = (List<string>)TempData["ImageNames"];
    foreach (var fileName in fileNames.ToList())
    {
        <p>@fileName</p>
    }
}
@if(TempData[“ImageNames”!=null)
{
列表文件名=(列表)TempData[“ImageNames”];
foreach(fileNames.ToList()中的var文件名)
{
@文件名

} }
列表不是空的,但没有打印任何内容

有什么不对劲吗

更新

[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");
}
以下是完整的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>

上载文件

@{List fileNames=(List)TempData[“ImageNames”];} @if(TempData[“ImageNames”!=null) { foreach(fileNames.ToList()中的var文件名) { @文件名

} }
另一次更新

[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");
}
[HttpPost]
公共操作结果上载(HttpPostedFileBase文件)
{
字符串文件名=Guid.NewGuid().ToString()+“.pdf”;
字符串路径=path.Combine(Server.MapPath(“~/Files/Pdf/”),文件名);
file.SaveAs(路径);
PdfReader reader=null;
iTextSharp.text.Document Document=null;
PdfCopy pdfCopyProvider=null;
PDFIImportedPage importedPage=null;
List pdfNames=新列表();
List imageNames=新列表();
尝试
{
读卡器=新的PDF读卡器(路径);

对于(int pageIndex=1;pageIndex可能是),我的语法会有点错误,因为我无法在这里打开visual studio,但据我所知,要在UI上显示变量,您需要在变量之前放置“:”。因此代码如下:

@if (TempData["ImageNames"] != null)
{
   List<string> fileNames = (List<string>)TempData["ImageNames"];
   foreach (var fileName in fileNames.ToList())
   {
      <p>@:fileName</p>
   }
}
@if(TempData[“ImageNames”!=null)
{
列表文件名=(列表)TempData[“ImageNames”];
foreach(fileNames.ToList()中的var文件名)
{
@:文件名

} }
逗我开心,试着做:

@{
    if (TempData["ImageNames"] != null) {
        List<string> fileNames = (List<string>)TempData["ImageNames"];
        foreach (var fileName in fileNames)
        {
            <p>@Html.Raw(fileName)</p>
        }
    }
}
@{
if(TempData[“ImageNames”!=null){
列表文件名=(列表)TempData[“ImageNames”];
foreach(文件名中的var文件名)
{
@Html.Raw(文件名)

} } }
@fileName
,使用这个我希望它能工作。

你怎么知道列表不是空的?@Andrei当然可以通过在铸造线上设置一个断点。好的,接下来的问题是-你检查了最终标记了吗?可能结果已经打印出来了,但是隐藏在视图中?请给我们看一下视图代码吗?发布填充
Te的代码mpData[“ImageNames”]
那么您的列表是空的。在if/foreach逻辑的不同点上使用
Hello 1

语句,可以很容易地验证和放大问题。@Sippy No它不是空的。@Henkholtman我试过@fileNames[0]

但没有打印任何内容。请尝试打印一些常量,这些常量告诉您已进入循环并退出@If()等。如何将TempData[“ImageNames”]转换为list。可能是因为foreach循环没有为此目的启动。