RouteConfig.cs Code:
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 {//Definition: Routing is the definition of how client requests are handled. Settings for route names: In routing settings, the route name is an optional input parameter, and the route name can produce a URL route, but it has no effect in route resolution. However, when a route name is used to produce a URL route, the routing module will quickly navigate to the route of the specified name, otherwise the query will be made until the corresponding route is found. Order: The route input order in the routing table should be entered in front of the usage frequency. Most commonly used in the first place, this method not only improves the efficiency of the production URL routing, but also improves the efficiency of routing resolution. Because in the process of routing parsing, once a matching route is found, the parsing is stopped. Note: When changing the location of a route, the order of the routing changes materially affects the result of the match. 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 {contro Ller = "Home", action = "Show", year =" n ", 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}); } }}
Controller code:
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 "view displayed under parent ..."; } public string Show (int years, int month, int day) { return year + "years" + month + "Month" + Day + "Days"; } }
Model:
You first use LINQ to connect to the database and then you build a class to start writing the method:
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 ();}}}
View Code:
@{Layout = null;} <! DOCTYPE html>Partial View Code:
@using Mvcapplication1.models@model list<car> <ul> @foreach (Car data in model) { <li>@ Data. Name @ (data. Price) </li> } </ul>
MVC Routing and Partial views