有ViewModel。
public class ProductsListViewModel
{
public List<Product> Products { get; set; }
public PagingInfo PagingInfo { get; set; }
public int CurrentCategory { get; set; }
}
我的产品(你实际上不需要它但是nts):
public class Product
{
[HiddenInput(DisplayValue = false)]
public int ProductID { get; set; }
[Required(ErrorMessage = "Please enter a product name")]
public string Name { get; set; }
[DataType(DataType.MultilineText)]
[Required(ErrorMessage = "Please enter a description")]
public string Description { get; set; }
[Required]
[Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")]
public decimal Price { get; set; }
[Required(ErrorMessage = "Please specify a category")]
public int Category { get; set; }
public byte[] ImageData { get; set; }
[HiddenInput(DisplayValue = false)]
public string ImageMimeType { get; set; }
}
我希望使用Knockout在我的页面上创建产品列表。 这是我的代码:
@using PerpetuumSoft.Knockout
@using Task13MVC.Domain
@model Task13MVC.Models.ProductsListViewModel
@{
ViewBag.Title = "List";
Layout = "~/Views/_MyViewStart.cshtml";
var ko = Html.CreateKnockoutContext();
}
<table>
<tbody>
@using (var items = ko.Foreach(m => Model.Products))
{
<tr>
<td>
<div class="item">
@using (items.If(m => m.ImageData != null))
{
<div style="float:left;margin-right:20px">
<img width="75" height="75" data-bind="attr:{'src':'@Url.Action("GetImage", "Product",new{id="How put there id?"})'"/>
</div>
}
<h3>@items.Html.Span(m => m.Name)</h3>
@items.Html.Span(m => m.Description);
<div class="item">
@items.Html.Button("Add to cart", "AddToCart", "Cart", new { });
</div>
<h4>@items.Html.Span(m => m.Price.ToString())</h4>
</div>
</td>
</tr>
}
</tbody>
</table>
@ko.Apply(Model)
upd我的结果: