Note that there are some explanatory texts on larger screens.

plurals
  1. POIf item already in Database, do different ActionResult in View - ASP.NET MVC4 EF5 HTML5
    primarykey
    data
    text
    <p>Say if im about to go to a simple Index() view for "Item". I have an action result in that index view which adds to the database (which would be a customer which has a library of items in the model). If the customer already has that chosen item in the database, I would like the ActionLink to show something like</p> <pre><code>@Html.ActionLink("Remove from Library", "RemoveFromLibrary", new {id=item.Id}) </code></pre> <p>Customer Model</p> <pre><code>public List&lt;LoanedItem&gt; Library { get; set; } </code></pre> <p>Item Index View</p> <pre><code>@Html.ActionLink("Add To Library", "AddToItems", new {id=item.Id}) </code></pre> <p>How would I go about this in the simplest possibile way? (am new to this)</p> <p>Thank you</p> <p><strong>PS. In a realistic scenario, its like an ebay watchlist. If an item is already in the watchlist, show text to remove from watch list for that item</strong></p> <p>EDIT:</p> <p>Im not sure whether to write code in Controller or the view itself but for the view, I tried adding the below and got stuck</p> <pre><code>public ActionResult Index() { Customer c = (Customer)Session["customer"]; var items = ItemRepository.GetItems().OfType&lt;Item&gt;(); var itemsLoanedToCustomer = customerRepository.GetItems(c.Id); foreach (Item i in items) { if (itemsLoanedToCustomer.Contains(i)) { } } return View(); } </code></pre> <p>FULL Index() View</p> <pre><code> @model IEnumerable&lt;MMC.Model.Item&gt; @{ ViewBag.Title = "Index"; } &lt;h2&gt;Tracks&lt;/h2&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.Artist) &lt;/th&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.Genre) &lt;/th&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.DailyLoanPrice) &lt;/th&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.Publisher) &lt;/th&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.ReleaseDate) &lt;/th&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.Title) &lt;/th&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.YearOfPublication) &lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; @foreach (var item in Model) { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Artist) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Genre) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.DailyLoanPrice) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Publisher) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.ReleaseDate) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Title) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.YearOfPublication) &lt;/td&gt; &lt;td&gt; @Html.ActionLink("Details", "Details", new { id=item.Id }) | @Html.ActionLink("Add To Library", "AddToTracks", new {id=item.Id}) &lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; </code></pre> <p>Customer Model</p> <pre><code> 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&lt;LoanedItem&gt; MediaLibrary { get; set; } public Bill balance { get; set; } public Customer() { if (Library == null) { Library = new List&lt;LoanedItem&gt;(); } } } </code></pre> <p>Item Model</p> <pre><code> 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload