如果项目已经在数据库中,在View - ASP中执行不同的ActionResult.Net mvc4 ef5 html5

本文关键字:ActionResult Net html5 ef5 mvc4 执行 项目 数据库 如果 View ASP | 更新日期: 2023-09-27 18:10:33

假设我要为"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})

我该如何用最简单的方式来做这件事呢?(我是新手)

谢谢

p。在现实的情况下,它就像一个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() 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; }
}

如果项目已经在数据库中,在View - ASP中执行不同的ActionResult.Net mvc4 ef5 html5

您从哪里获得要显示操作链接的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>();
    }
}