Asp.net MVC學習日記八(JQuery和分部視圖,查看詳細)

來源:互聯網
上載者:User

1、建立類Product和ProductRepository

    public class Product
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public string Description { get; set; }
        public string Sku { get; set; }
        public DateTime CreateDate { get; set; }
        public bool InStock { get; set; }
    }

 public class ProductRepository
    {
        public List<Product> GetProducts()
        {
            List<Product> products = Builder<Product>
                .CreateListOfSize(20)
                .Build()
                .ToList();

            return products;
        }

        public Product GetProductByID(int productId)
        {
            Product product = Builder<Product>
                .CreateNew()
                .With(x => x.ProductId = productId)
                .And(x => x.Description = @"Hello World")
                .Build();

            return product;
        }
    }

2、在HomeController中添加

       public ActionResult Product()
        {
            ViewData["Products"] = new ProductRepository().GetProducts();
            return View();
        }

        public ActionResult ProductDetail(int id)
        {
            ViewData["Product"] = new ProductRepository().GetProductByID(id);
            return View();
        }

        public ActionResult GetProductDetail(int id)
        {
            ViewData["Product"] = new ProductRepository().GetProductByID(id);
            return View();
        }

3、實現Product和ProductDetail類,同時實現GetProductDetail部分類別

4、Product.aspx中添加

<script type="text/javascript">

        $(function () {
            $('a.list-item').click(function () {
                $("#detail").html('');
                $('#detail').load('<%: Url.Action("GetProductDetail") %>', { id: this.id });
                return false;
            });
        });
    </script>
    <style type="text/css">
        .list-item { cursor: pointer; }
    </style>

   

 <div id="detail"></div>  
    <% List<Product> products = ViewData["Products"] as List<Product>;
        foreach (Product product in products)
        { %>
    <div class="display-field">
        <%: String.Format("{0:c}", product.Price)%>
        <%= Html.ActionLink(product.Name, "ProductDetail", new { @id = product.ProductId }, new { @id = product.ProductId, @class = "list-item" })%>
     </div>
        <% } %>

5、ProductDetail.aspx中添加

   <% Html.RenderPartial("GetProductDetail"); %>

6、GetProductDetail.ascx中添加

<%
Product p = ViewData["Product"] as Product;
%>
<div class="display-label">Id: </div>
 <div class="display-field"><%: p.ProductId %></div>
 <div class="display-label">Name: </div>
 <div class="display-field"><%: p.Name %></div>
 <div class="display-label">Price: </div>
 <div class="display-field"><%: String.Format("{0:c}", p.Price) %></div>
 <div class="display-label">SKU: </div>
 <div class="display-field"><%: p.Sku %></div>
 <div class="display-label">Created: </div>
 <div class="display-field"><%: p.CreateDate %></div>
 <div class="display-label">In Stock: </div>
 <div class="display-field"><%: p.InStock %></div>
 <div class="display-label">Desc: </div>
 <div class="display-field"><%: p.Description %></div>
 <hr />

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.