简体   繁体   English

局部视图未加载数据

[英]Partial View not loading data

I am trying to load a partial view of inside a tab but its not showing data. 我正在尝试加载选项卡内部的部分视图,但未显示数据。

I am using the following code can I not just do a loop using razor code this is in a partial view which I wish to load in from another view 我正在使用以下代码,我不能只使用剃刀代码进行循环,这是在局部视图中,我希望从另一个视图中加载

@model IEnumerable<solitude.models.ProductImages>


@{
    ViewData["Title"] = "ProductPicturesList";
    Layout = "~/Views/Shared/_LoginAdminLte.cshtml";
}

<h2>ProductPicturesList</h2>



<table class="table">
    <thead>
        <tr>
            <th>
               Picture Title
            </th>


            <th>
                Image
            </th>


    </thead>

    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Title)
                </td>
            </tr>


        <td>
            <a asp-action="Edit" asp-route-id="@item.ProductID">Edit</a> |
            <a asp-action="Details" asp-route-id="@item.ProductID">Details</a> |
            <a asp-action="Delete" asp-route-id="@item.ProductID">Delete</a>
        </td>

        }
        </tbody>
    </table>

Its cause in the main list I am using a view model but I want to show a list of pictures above the form upload what would my best way of doing this be as obv it is not returning anyresults I am using a controller for my main page. 其原因在主列表中,我正在使用视图模型,但我想在表单上传上方显示一张图片列表,这是我最好的方法,因为它不返回任何结果,我正在为主页使用控制器。

@model solitude.models.Models.ViewModels.ProductImageVm 
@*
    For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@

@Html.PartialAsync("_ProductPicturesList.cshtml")

<div class="form-group">

 <form asp-controller="Products" asp-action="FileUpload" asp-route-returnurl="@ViewData["ReturnUrl"]" enctype="multipart/form-data" method="post" class="form-horizontal" role="form">



    <input asp-for="Title" />
    <input asp-for="ProductId" type="hidden" />
    <input asp-for="Image" />
    <input type="submit" />
</form>

Edit 2 My Product Images as a class should this be changed 如果要更改此类别,请编辑2个我的产品图片

public   class ProductImages
{


        [Key]
        public int ProductImageId { get; set; }

        public int ProductID { get; set; }


        public string ProductImageTitle { get; set; }
        public string ProductImageUploadUrl { get; set; }
        public string ProductImageRealPath { get; set; }

        public string ServerIpAddress { get; set; }
        public string ProductImageAltTag { get; set; }

        public int DisplayOrder { get; set; }

        public string Image { set; get; }

    }
}

Your partial view is strongly typed to a collection of ProductImages . 您的部分视图被严格键入到ProductImages的集合中。 But in your main view when you are calling this partial view, you are not passing the model (which is the collection of ProductImage objects) to this partial view. 但是在主视图中,当您调用此局部视图时,没有将模型(ProductImage对象的集合)传递给该局部视图。 If you are not explcitily passing the model, it will try to use the model for the parent view. 如果没有明确传递模型,它将尝试将模型用于父视图。 In your case your parent view is strongly ProductImageVm view model class. 在您的情况下,您的父视图强烈是ProductImageVm视图模型类。 So it is not maching with what the partial view is expecting. 因此,它并不能满足部分视图的期望。

The solution is to pass a valid collection of ProductImages. 解决方案是传递一个有效的ProductImages集合。 If your view model has a collection property of that type you can do that 如果您的视图模型具有该类型的集合属性,则可以执行此操作

@await Html.PartialAsync("_ProductPicturesList.cshtml",Model.Images)

Assuming Images of type IEnumerable<solitude.models.ProductImages> 假设Images类型为IEnumerable<solitude.models.ProductImages>

Ideally it is not a great idea to mix entity classes with view models. 理想情况下,将实体类与视图模型混合并不是一个好主意。 So i would create a view model class for the ProductImage partial view and use that as the property 所以我会为ProductImage部分视图创建一个视图模型类,并将其用作属性

public class ProductImg
{
  public string Title { set;get;}
  public string FileName  { set;get;}
  // or path as needed
}
public class EditProductImageVm
{
   public string Title { set;get;} //for the new item
   public IFormFile Image { set;get; }  //for the new item

   public IEnumerable<ProductImg> Images { set;get;}
}

Now make sure main view is not strongly typed to EditProductImageVm and your partial view is strongly typed to IEnumerable<ProductImg> . 现在,请确保主视图的类型不强于EditProductImageVm,而部分视图的类型也强于IEnumerable<ProductImg> Also you need to await the call to PartialAsync method 另外,您还需要等待PartialAsync方法的调用

@model YourNameSpaceGoesHere.EditProductImageVm 
<div>
   @await Html.PartialAsync("_ProductPicturesList.cshtml",Model.Images);
</div>
<form asp-controller="Products" asp-action="FileUpload" enctype="multipart/form-data"
                                 method="post" >
    <input asp-for="Title" />
    <input asp-for="Image" />
    <input type="submit" />
</form>

And your partial view will be 您的部分观点是

@model IEnumerable<YourNameSpaceGoesHere.ProductImg>
<h3>Images</h3>
<table class="table table-condensed table-bordered">
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
             <td>
                <!-- adjust the below img src path as needed -->
                <img src="@Url.Content("~/uploads/"+item.FileName)"/>
             </td>
        </tr>
    }
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM