簡體   English   中英

如果項目已在數據庫中,請在視圖中執行不同的ActionResult-ASP.NET MVC4 EF5 HTML5

[英]If item already in Database, do different ActionResult in View - ASP.NET MVC4 EF5 HTML5

假設我要轉到“項目”的簡單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監視列表。 如果某個項目已在監視列表中,則顯示要從該項目的監視列表中刪除的文本

編輯:

我不確定是否要在Controller或視圖本身中編寫代碼,但是對於視圖,我嘗試添加以下內容並卡住了

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; }
}

您從何處獲取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>();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM