繁体   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