mvc 路由及部分視圖

RouteConfig.cs代碼:html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication1
{
    public class RouteConfig
    {
        //定義:路由是定義如何處理客戶端請求。

        //路由名稱的設置: 在路由設置中,路由名稱是可選輸入參數,路由名稱可生產URL路由,可是在路由解析中沒有什麼做用。不過當使用路由名稱來生產URL路由的時候,路由模塊將快速定位到指定名稱的路由,不然將會進行查詢,直到找到對應的路由。

        //順序:路由表中的路由輸入順序應該按使用頻率從前向後輸入。最經常使用的放在最前面,此法方法不只提升生產URL路由的效率,並且也提升路由解析的效率。由於在路由解析的過程當中,一旦找到匹配的路由,就中止解析。

        //注意:在改變路由存放位置時,路由的次序改變是否實質性的影響匹配的結果。 
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
               name: "Default1",
               url: "find/{low}-{upp}",
               defaults: new { controller = "Home", action = "FindByPrice", low=0,upp=100 }
           );
            routes.MapRoute(
               name: "Default2",
               url: "k{year}/{month}/{day}",
               defaults: new { controller = "Home", action = "Show", year = "2015", month = "1", day = "1" }
           );

            routes.MapRoute(
                name: "Default3",
                url: "{k}-{parent}",
                defaults: new { controller = "Home", action = "Show", parent = "" }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

  控制器代碼:jquery

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult FindByPrice(decimal low, decimal upp)
        {
            List<Car> list = new CarDA().SelectByPrice(low, upp);
            return PartialView(list);
        }
        public string ShowParent(string parent)
        {
            return "父級下顯示的視圖...";
        }
        public string Show(int year, int month, int day)
        {
            return year + "年" + month + "月" + day + "日";
        }

    }
}

  

模型:數據庫

首先要用linq鏈接數據庫 而後建一個類開始寫方法:url

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1.Models
{
    public class CarDA
    {
        private MyDBDataContext _context = new MyDBDataContext();
        public List<Car> Select()
        {
            return _context.Car.ToList();
        }
        public List<Car> SelectByPrice(decimal low, decimal upp)
        {
            var query = _context.Car.Where(p => p.Price >= low && p.Price <= upp);
            return query.ToList();
        }
    }
}

  

視圖代碼:spa

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Script/jquery-1.7.1.min.js"></script>
    <script>
        function test() {
            var low = $("#low").val();
            var upp = $("#upp").val();
            $("#dd").load("/find/" + low + "-" + upp);
        }
    </script>
</head>
<body>
    <div>
        <div>
        @Html.ActionLink("此處走第二個路徑 ", "Show", new { year=2015, month=7,day=11})<br>
        @Html.ActionLink("此處走第三個路徑 ", "ShowParent", new {parent=""})<br>
        @using (Html.BeginForm("FindByPrice", "Home"))
        {
            <div>
            最低價格:@Html.TextBox("low")
            最高價格:@Html.TextBox("upp")
            <input type="button" onclick="test()" value="查詢" />
           </div>
        }
    </div>
    <div id="dd"></div>
    </div>
</body>
</html>

  部分視圖代碼:orm

@using MvcApplication1.Models
@model List<Car>

        <ul>
            @foreach(Car data in Model){
            <li>@data.Name @(data.Price)</li>
            }
        </ul>
相關文章
相關標籤/搜索