淘汰MVC foreach

时间:2014-05-28 13:25:20

标签: c# asp.net-mvc-4 razor knockout.js foreach

有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我的结果:

enter image description here

  1. 为什么它不会前进,只有一次。
  2. 为什么没有结果(items.Html.Span必须在哪里)。
  3. 如何将productId放入图像源链接属性。

0 个答案:

没有答案