如果我即将转到"项目"的简单索引()视图。 我在索引视图中有一个操作结果,它添加到数据库(这将是一个在模型中有一个项目库的客户)。如果客户已在数据库中拥有所选项目,我希望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();
}
完整索引()查看
@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; }
}
答案 0 :(得分: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>();
}
}