0

「アイテム」の単純な 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();
    }

FULL Index() ビュー

    @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 に答える