0

假设我要转到“Item”的简单 Index() 视图。我在该索引视图中有一个操作结果,它添加到数据库中(这将是一个在模型中具有项目库的客户)。如果客户已经在数据库中拥有该选择的项目,我希望 ActionLink 显示类似

@Html.ActionLink("Remove from Library", "RemoveFromLibrary", new {id=item.Id})

客户模型

public List<LoanedItem> Library { get; set; }

项目索引视图

@Html.ActionLink("Add To Library", "AddToItems", new {id=item.Id})

我将如何以最简单的方式解决这个问题?(我是新手)

谢谢

PS。在现实场景中,它就像一个 ebay 监视列表。如果某个项目已在关注列表中,则显示要从该项目的关注列表中删除的文本

编辑:

我不确定是在控制器还是视图本身中编写代码,但是对于视图,我尝试添加以下内容并卡住了

public ActionResult Index()
    {
        Customer c = (Customer)Session["customer"];
        var items = ItemRepository.GetItems().OfType<Item>();
        var itemsLoanedToCustomer = customerRepository.GetItems(c.Id);
        foreach (Item i in items)
        {
            if (itemsLoanedToCustomer.Contains(i))
            {

            }
        }
        return View();
    }

完整索引()视图

    @model IEnumerable<MMC.Model.Item>

@{
    ViewBag.Title = "Index";
}

<h2>Tracks</h2>

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Artist)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Genre)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DailyLoanPrice)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Publisher)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ReleaseDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.YearOfPublication)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Artist)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Genre)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DailyLoanPrice)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Publisher)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ReleaseDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.YearOfPublication)
        </td>
        <td>
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Add To Library", "AddToTracks", new {id=item.Id})

        </td>
    </tr>
}

</table>

客户模型

    public class Customer
{

    public int Id { get; set; }
    public string ForeName { get; set; }
    public string SurName { get; set; }
    public Address address { get; set; }
    public string Email { get; set; }
    public string Telephone { get; set; }
    public string Mobile { get; set; }
    public List<LoanedItem> MediaLibrary { get; set; }
    public Bill balance { get; set; }

    public Customer()
    {
        if (Library == null)
        {
            Library = new List<LoanedItem>();
        }
    }
}

项目型号

    public class Item
{
    public int Id { get; set; }
    public double DailyLoanPrice { get; set; }

    [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
    public DateTime LastTimeBorrowed { get; set; }
    public int NumberOfTimeBorrowed { get; set; }
    public string Publisher { get; set; }
    //rating
    [DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
    public DateTime ReleaseDate { get; set; }
    public string Title { get; set; }
    public double TotalSalesIncome { get; set; }
    public int YearOfPublication { get; set; }
}
4

1 回答 1

0

您从哪里获得要显示操作链接的 LibraryItems 列表?如果它们在您的模型中,那么;

在视图中检查用户是否已经拥有该项目并相应地显示操作链接。

@(if(Model.Customers.Items.Select(x => x.Id).Intersect(Model.Items.Select(x => x.Id)).Any())
{
    Html.ActionLink("Remove from Library", "RemoveFromLibrary", new {id=item.Id});
}
else
{
    Html.ActionLink("Add To Library", "AddToItems", new {id=item.Id});
})

此外,如果您使用的是 EF,您可以执行类似的操作

public ActionResult Index()
{
    Customer c = (Customer)Session["customer"];

    // Items is the navigation property.
    var customerItems = customerRepository.GetItems(c.Id).Items;

    return View();
}

使用以下 ViewModel

public class CustomerItem
{
    public List<Item> Items;
    public List<Customer> Customers;

    public CustomerItem()
    {
        Items = new List<Items>();
        Customers = new List<Items>();
    }
}
于 2013-04-23T10:27:22.677 回答