mvc 路由及部分视图

RouteConfig.cs代码:

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 }
            );
        }
    }
}

  控制器代码:

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连接数据库 然后建一个类开始写方法:

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();
        }
    }
}

  

视图代码:

@{
    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" οnclick="test()" value="查询" />
           </div>
        }
    </div>
    <div id="dd"></div>
    </div>
</body>
</html>

  部分视图代码:

@using MvcApplication1.Models
@model List<Car>

        <ul>
            @foreach(Car data in Model){
            <li>@data.Name @(data.Price)</li>
            }
        </ul>

  

转载于:https://www.cnblogs.com/Mr-xue/p/4652776.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值