C# MVC-从Azure httppost返回CSHTML中的文件属性

C# MVC-从Azure httppost返回CSHTML中的文件属性,c#,asp.net-mvc,asp.net-mvc-4,azure,C#,Asp.net Mvc,Asp.net Mvc 4,Azure,我有一个问题,我无法返回上传到网站并存储在Azure上的文件的文件名和文件大小,即使控制器中存在变量名 @* Upload.cshtml *@ @model IList<FileDisplayModel> my CSHTML的一个片段如下所示: <table class="table table-striped table-hover "> <thead> <tr> <th>FIlename</th>

我有一个问题,我无法返回上传到网站并存储在Azure上的文件的文件名和文件大小,即使控制器中存在变量名

@* Upload.cshtml *@
@model IList<FileDisplayModel>
my CSHTML的一个片段如下所示:

<table class="table table-striped table-hover ">  
<thead>
<tr>
    <th>FIlename</th>
    <th>Modified</th>
    <th>File Size</th>
    <th>Download File</th>
    <th>Delete File</th>
    </tr>
</thead>

<tbody>
@foreach (var item in Model)
{
<tr>
    <td>@item.Name</td>
    <td>Modified</td>
    <td>@item.ContentLength</td>
    <td><a id="@item" href="@item" onclick="downloadFile('@item')">Download</a></td>
    <td><a id="@item" href="#" onclick="deleteFile('@item')">Delete</a></td>
</tr>
}
</tbody>
</table>

文件名
被改进的
文件大小
下载文件
删除文件
@foreach(模型中的var项目)
{
@项目名称
被改进的
@item.ContentLength
}
正如你们所看到的,我有item.Name,但我已经尝试了很多其他的,我将在下面总结我所尝试的。值得一提的是,我可以下载并删除该文件

我的控制器最重要的部分是:

public class HomeController : Controller
{

    BlobStorageService _blobStorageService = new BlobStorageService();

    public ActionResult Upload()
    {
        CloudBlobContainer blobContainer = _blobStorageService.GetCloudBlobContainer();
        List<string> blobs = new List<string>();
        foreach (var blobItem in blobContainer.ListBlobs())
        {
            blobs.Add(blobItem.Uri.ToString());
        }
        return View(blobs);
    }

    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase file)
    {
        if (file.ContentLength > 0)
        {
            CloudBlobContainer blobContainer = _blobStorageService.GetCloudBlobContainer();
            CloudBlockBlob blob = blobContainer.GetBlockBlobReference(file.FileName);
            blob.UploadFromStream(file.InputStream);
        }
        return RedirectToAction("Upload");
    }
公共类HomeController:控制器
{
BlobStorageService _BlobStorageService=新BlobStorageService();
公共行动结果上载()
{
CloudBlobContainer blobContainer=\u blobStorageService.GetCloudBlobContainer();
List BLOB=新列表();
foreach(blobContainer.ListBlobs()中的var blobItem)
{
Add(blobItem.Uri.ToString());
}
返回视图(BLOB);
}
[HttpPost]
公共操作结果上载(HttpPostedFileBase文件)
{
如果(file.ContentLength>0)
{
CloudBlobContainer blobContainer=\u blobStorageService.GetCloudBlobContainer();
CloudBlockBlob blob=blobContainer.GetBlockBlobReference(file.FileName);
UploadFromStream(file.InputStream);
}
返回重定向操作(“上载”);
}
如您所见,ContentLength(文件大小和文件名)存在于HomeController上下文中

为了让它工作,我尝试了以下不同的例子

@item.FileName,@item.file.FileName,@item.HttpPostedFileBase.FileName @file.FileName、@item.FileName-其他的都一样


如果您知道答案,您能告诉我您在哪里找到它的来源吗?我习惯于使用Python,Python有其模块的文档,但这有很大不同

问题在于用于呈现GET Upload视图的ViewModel

[HttpGet]
public ActionResult Upload() {      
    List<string> blobs = new List<string>();
    // add URI strings to list ...
    return View(blobs, "Upload");
}
在cshtml中使用
@model
指令可以让IDE检查您是否从控制器传递了正确的模型

@* Upload.cshtml *@
@model IList<FileDisplayModel>
@*Upload.cshtml*@
@模型IList
可在MSDN上找到.net的文档:


以下是我用于上传图像的教程:

所以我应该创建一个名为FileDisplay的新模型?是的,在控制器中创建并填充一个ViewModel,然后使用它渲染视图。