这个Razor代码有什么问题?

时间:2012-02-10 15:01:20

标签: c# asp.net-mvc-3 razor

@using System.Configuration
@using UI.AuctionService
@using UI.Common
@using UI.Helpers

@model UI.Models.AuctionFrontendListViewModel

@{
    ViewBag.Title = "Auction List";
}

<h2>@Model.Title</h2>

<table>
    <tr>
        <th>
            Image
        </th>
        <th>
            Type
        </th>
        <th>
            Auction title
        </th>
        <th>
            Starts
        </th>
        <th>
            Ends
        </th>
        <th>
            Is featured
        </th>
        <th>
            Bid count
        </th>
        <th>
            Creation time
        </th>
        <th></th>   
    </tr>

@foreach (var auction in Model.Auctions)
{
    var type = string.Empty;
    if (auction.Auction is LubAuction)
    {
        type = "Lowest unique wins";
    }
    else if (auction.Auction is EsfAuction)
    {
        type = "Highest wins";
    }

    string imagesFolderPath = HttpContextHelper.GetPathInServer(ConfigurationManager.AppSettings["ImagesFolderPath"]);
    string itemImagesFolderPath = Path.Combine(imagesFolderPath, ImageType.Item + @"\\" + auction.Auction.InventoryReference);
    string chosenImage = string.Empty;
    if (Directory.Exists(itemImagesFolderPath))
    {
        string[] files = Directory.GetFiles(itemImagesFolderPath);
        if (files.Length > 0)
        {
            chosenImage = files[0];
        }
    }

    <tr>
       <td>
            <img src="@chosenImage" />
       </td>
       <td>
            @type
       </td>
       <td>
            @Html.DisplayFor(modelItem => auction.Auction.Title)
        </td>
        <td>
            @DateTimeHelper.LocalDateTime(auction.Auction.Starts)
        </td>
        <td>
            @DateTimeHelper.LocalDateTime(auction.Auction.Ends)
        </td>
        <td>
            @Html.DisplayFor(modelItem => auction.Auction.IsFeatured)
        </td>
        <td>
            @Html.DisplayFor(modelItem => auction.Auction.BidCount)
        </td>
        <td>
            @DateTimeHelper.LocalDateTime(auction.Auction.AddedDate)
        </td>
        <td>
           @Html.ActionLink("Details", "Details", new { id = auction.Auction.Id })
        </td>
    </tr>
}

</table>
<div class="pager">
@Html.PageLinks(Model.PagingInfo, x => Url.Action(Model.Action, new { page = x }))
</div>

1 个答案:

答案 0 :(得分:2)

如果图像是问题,我建议采取不同的方式。您的Razor视图可能不应该负责搜索服务器以查找图像;考虑通过模型或ViewData将信息推送到视图中。

另外,为什么要将@"\\"传递给Path.Combine?您正在为路径添加双斜杠。让Path.Combine处理这个。我想这可能是你的主要问题。执行以下操作之一:

string itemImagesFolderPath = Path.Combine(imagesFolderPath, ImageType.Item + @"\" + auction.Auction.InventoryReference);

或者,更优选

string itemImagesFolderPath = Path.Combine(imagesFolderPath, ImageType.Item, auction.Auction.InventoryReference);