zoukankan      html  css  js  c++  java
  • 在ASP.NET MVC3 中使用OData

    1. 新建一个asp.net mvc3 应用程序项目
    2. 添加服务引用
    3. 输入地址:http://services.odata.org/Northwind/Northwind.svc/ 转到并添加NorthwindService命名空间
    4. 新建控制器
    5. 按F6进行编译
    6. 添加强类型分部视图ProductList
    7. 替换Views/Product/ProductList.cshtml代码
      View Code
      @model IEnumerable<ODataMvcApplication.NorthwindService.Product>
      
      <table>
          <tr>
              <th>
                  ProductName
              </th>
              <th>
                  SupplierID
              </th>
              <th>
                  CategoryID
              </th>
              <th>
                  QuantityPerUnit
              </th>
              <th>
                  UnitPrice
              </th>
              <th>
                  UnitsInStock
              </th>
              <th>
                  UnitsOnOrder
              </th>
              <th>
                  ReorderLevel
              </th>
              <th>
                  Discontinued
              </th>
          </tr>
      
      @foreach (var item in Model) {
          <tr>
              <td>
                  @Html.DisplayFor(modelItem => item.ProductName)
              </td>
              <td>
                  @Html.DisplayFor(modelItem => item.SupplierID)
              </td>
              <td>
                  @Html.DisplayFor(modelItem => item.CategoryID)
              </td>
              <td>
                  @Html.DisplayFor(modelItem => item.QuantityPerUnit)
              </td>
              <td>
                  @Html.DisplayFor(modelItem => item.UnitPrice)
              </td>
              <td>
                  @Html.DisplayFor(modelItem => item.UnitsInStock)
              </td>
              <td>
                  @Html.DisplayFor(modelItem => item.UnitsOnOrder)
              </td>
              <td>
                  @Html.DisplayFor(modelItem => item.ReorderLevel)
              </td>
              <td>
                  @Html.DisplayFor(modelItem => item.Discontinued)
              </td>
          </tr>
      }
      
      </table>
    8. 替换Controllers/ProductController.cs代码
      View Code
      using ODataMvcApplication.NorthwindService;
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Mvc;
      
      namespace ODataMvcApplication.Controllers
      {
          public class ProductController : Controller
          {
              public ActionResult Index()
              {
                  return View();
              }
      
              public PartialViewResult Products(int id)
              {
                  var context = new NorthwindEntities(new Uri("http://services.odata.org/Northwind/Northwind.svc/"));
                  var products = from product in context.Products where product.CategoryID == id select product; return PartialView("ProductList", products.ToList());
              }
      
              static public List<Category> Categories() { var context = new NorthwindEntities(new Uri("http://services.odata.org/Northwind/Northwind.svc/")); return context.Categories.ToList(); }
          }
      
      
      }
    9. 添加Index视图
    10. 替换Views/Product/Index.cshtml代码
      View Code
      @{
          Layout = null;
      }
      <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")"></script>
      <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
      <script type="text/javascript">
          $(document).ready(function () {
              $("#category").change(function () { loadView($(this).val()); }); loadView("1");
          });
          function loadView(cat)
          {
              $("#content").html("加载中。。。。");
              $.get('@Url.RouteUrl("default", new { controller = "Product", action = "Products" })', { id: cat }, function (data) {
                  $("#content").html(data);
              });
          }
      
      </script>
      <div>
          <h1>Products</h1>
          <div>
              <div>Category:</div>
              @Html.DropDownList("category", ODataMvcApplication.Controllers.ProductController.Categories().Select(c => new SelectListItem { Text = c.CategoryName, Value = c.CategoryID.ToString() }))    </div>
          <hr />
          <div id="content">加载中....</div>
      </div>
    11. 预览效果
    作者:王春明 出处:http://wangchunming.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    leetcode 198. House Robber
    leetcode 35. Search Insert Position
    一文读懂机器学习,大数据/自然语言处理/算法全有了……
    成都Uber优步司机奖励政策(1月30日)
    北京Uber优步司机奖励政策(1月30日)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(1月30日)
    成都Uber优步司机奖励政策(1月29日)
    北京Uber优步司机奖励政策(1月29日)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(1月29日)
    成都Uber优步司机奖励政策(1月28日)
  • 原文地址:https://www.cnblogs.com/wangchunming/p/3039214.html
Copyright © 2011-2022 走看看