简体   繁体   English

强类型视图(ASP.NET MVC5)中的多个局部视图 无法获取相对路径

[英]Multiple partial views in a strongly typed view (ASP.NET MVC5) ? Unable to get relative Path In View

I am having an issue that after saving relative path I cannot get picture in a list, and having error: 我遇到一个问题,保存相对路径后,我无法在列表中获取图片,并且出现错误:

CS1061 CS1061
'IEnumerable<tbl_Advertisement>' does not contain a definition for 'Imagepath' and no extension method 'Imagepath' accepting a first argument of type 'IEnumerable<tbl_Advertisement>' could be found (are you missing a using directive or an assembly reference?) GawadarHubUI C:\\Users\\Omer FarooQ\\Desktop\\GawadarHubistan\\GawadarHubUI\\GawadarHubUI\\Areas\\Common\\Views\\SampleViewAD\\Index.cshtml 'IEnumerable<tbl_Advertisement>'不包含'Imagepath'的定义,并且找不到扩展方法'Imagepath'接受类型为'IEnumerable<tbl_Advertisement>'的第一个参数(您是否缺少using指令或程序集引用?) GawadarHubUI C:\\ Users \\ Omer FarooQ \\ Desktop \\ GawadarHubistan \\ GawadarHubUI \\ GawadarHubUI \\ Areas \\ Common \\ Views \\ SampleViewAD \\ Index.cshtml
73 73
Active 活性

This rrror in just view line: @Html.DisplayNameFor(model => Model.ImagePath) that after type Model . 该错误仅在查看行: @Html.DisplayNameFor(model => Model.ImagePath)之后键入Model I cannot get the the property Name ImagePath , rather it's showing me Extension methods and methods 我无法获得属性Name ImagePath ,而是向我展示了扩展方法和方法

Controller CODE 控制器代码

using BLL;
using BOL;
using DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GawadarHubUI.Areas.Common.Controllers
{
    [AllowAnonymous]
    public class SampleViewADController : Controller
    {
        GawadarHubDbEntities db = new GawadarHubDbEntities();

        private AdBl objbl;
        public SampleViewADController()
        {
            objbl = new AdBl();
        }


        // GET: Common/SampleViewAD
        [HttpGet]
        public ActionResult Index()
        {
            try
            {
                var urls = objbl.GetAll().Where(x => x.isapproved == "A").ToList();
                return View(urls);
            }
            catch (Exception e1)
            {
                TempData["Msg"] = "Approve Failed : " + e1.Message;
                return RedirectToAction("Index");
            }
        }
    }
}

View code: 查看代码:

@model IEnumerable<BOL.tbl_Advertisement>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>
@if (TempData["Msg"] != null)
{
    <div class="alert alert-dismissable alert-info">
        <button type="button" class="close" data-dismiss="alert">×</button>
        @TempData["Msg"].ToString()
    </div>}

<p>
    @Html.ActionLink("SampleViewADController", "urls")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.adtitle)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.adsociety)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.addesc)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.isapproved)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.contactNumber)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ImagePath)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.price)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Size)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.tbl_Category.cname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.tbl_User.useremail)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.adtitle)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.adsociety)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.addesc)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.isapproved)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.contactNumber)
            </td>
            <td>
                @Html.DisplayFor(model => Model.ImagePath)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.price)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Size)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.tbl_Category.cname)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.tbl_User.useremail)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.adid }) |
                @Html.ActionLink("Details", "Details", new { id = item.adid }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.adid })
            </td>
        </tr>
    }

</table>

Try to replace 尝试更换

@Html.DisplayFor(model => Model.ImagePath)

with

<img src= "@Url.Content(item.ImagePath)" alt="Image" />

Should work. 应该管用。

Your define that your model is a collection: 您定义模型是一个集合:

@model IEnumerable<BOL.tbl_Advertisement>

So every time you write Model. 因此,每次您编写Model. , you are referencing this collection. ,您正在引用此集合。

The collection ( IEnumerable<BOL.tbl_Advertisement> ) has no property for Imagepath , as the error message tells you. 集合IEnumerable<BOL.tbl_Advertisement> )不具有Imagepath属性,因为错误消息会告诉您。

IEnumerable<tbl_Advertisement> does not contain a definition for Imagepath IEnumerable<tbl_Advertisement>不包含Imagepath的定义

Rather, the elements ( tbl_Advertisement ) of the collection have this property. 而是,集合的元素tbl_Advertisement )具有此属性。

This is what Stephen and Igor meant when they wrote that you need to access the Imagepath through the elements of the collection. 这就是Stephen和Igor在编写您需要通过集合的元素访问Imagepath时的意思。

Either by looping over the collection: 通过遍历集合:

@foreach (var item in Model) {
   <img src= "@Url.Content(item.ImagePath)" alt="Image" />
}

Or for the first element only: 或仅对于第一个元素:

var imagePath = Model.ElementAt(0).ImagePath;

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

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