asp.net-mvc - 另一个 Controller MVC5的显示 View

标签 asp.net-mvc razor azure asp.net-mvc-5

我开始使用 .NET 进行开发,但有一些问题。

我创建了一个将图像上传到 Azure 的 View 。该 View 包含在名为 Document 的 Controller 中。

我想要的是在另一个 Controller View 中显示此 View 。该 View 单独工作完美,但是当我尝试引用它时,它给了我一个错误,我仍然不知道如何解决。

这是 View “Upload.cshtml”

@{
    ViewBag.Title = "Upload";
}

<p>
    @using (Html.BeginForm("Upload", "Documento", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <input type="file" id="fileToUpload" name="image" />
        <input type="submit" id="btnSubmit" value="Upload" />
    }
</p>

<ul style="list-style-type: none; padding: 0;">
    @foreach (var item in Model)
    {
        <li>
            <img src="@item" alt="images" width="100" height="100" />
            <a id="@item" href="#" onclick="deleteImage('@item');">Delete</a>
        </li>
    }
</ul>

<script type="text/jscript">
    //get file size
    function deleteImage(item) {
        try {
            var url = "/Documento/DeleteImage";

            $.post(url, { Name: item }, function (data) {
                window.location.href = "/Documento/Upload";
                alert(data);
            });
        }
        catch (e) {
            alert("Error is :" + e);
        }
    }
</script>

这就是我尝试从另一个 Controller 索引 View 调用 View 的方法:

@RenderPage("~/Views/Documento/Upload.cshtml");
@RenderBody();

我得到的错误是因为“@foreach(var item in Model)”句子。

我应该怎么做?

最佳答案

您的 View 顶部似乎缺少模型。像这样的事情:

@model MyProject.Models.MyModel

其次,您的 foreach 循环需要 IEnumerable 类型。您的模型是 IEnumerable 还是 @Model.SomeIEnumerable?

最后,循环中的任何 @item 都应该具有单独的 img src 和 anchor id 属性。

您显示的代码不完整,或者您存在模型问题。以下是如何执行我认为您正在寻找的操作的任何示例。

查看模型

public class MyModel
{
  public string ProductId {get;set;}
  public string ProductSrc {get;set;}
}

查看

@model IEnumerable<MyModel>
<ul>
@foreach(item in Model)
{
 <li>
   <img src="@item.ProductSrc" />
   <a id="@item.ProductId">Delete</>
 </li>
}

关于asp.net-mvc - 另一个 Controller MVC5的显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22205828/

相关文章:

asp.net-mvc - 获取 MVC3 中选中复选框的 ID

javascript - ASP.NET Core Url.Action 返回空字符串

c# - 在单独的 div 中的文本区域中间对齐标签

azure - Windows Azure 发布网站 - "Unable to get subscription information. An item with the same key has already been added"

c# - 如何以编程方式获取 Azure Batch 节点中的核心数量?

c# - .NET 4.0、MVC 2、 Entity Framework 4 和存储库模式

c# - Kendo Grid 绑定(bind)到字符串列表

git - 远程计算机上 git 中的多个包文件

c# - 无法从 Asp.Net MVC 连接到远程服务器

asp.net-mvc - 在 MVC5 中的 Owin Identity 中重用 regenerateIdentityCallback 中的声明