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" onclick="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>
时间: 2024-10-13 11:56:04