- document.getElementById('ManagePlans').setAttribute("class", "active");
- @Html.DropDownList("ddlClients", (SelectList)ViewBag.ClientList, "-Select-", new { @class = "userType", @id = "ddlClients" })
2. Convert List to IEnumerable<SelectListItem>
ViewBag.AvaiableEnums = dynamicTextEnumsAvaiable.Select(x =>
new SelectListItem()
{
Text = x.ToString()
});
Convert var to IEnumerable<SelectListItem>
var city = from c in lstCity where c.Country == mCountry || c.Country == 0 select c;
return Json(new SelectList(city.ToArray(), "ID", "Name"), JsonRequestBehavior.AllowGet);
3. Constraining a Route Using a Regular Expression
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { controller = "^H.*" }, new[] { "URLsAndRoutes.Controllers" }); } } Constraints are defined by passing them as a parameter to the MapRoute method. Like default values, constraints are expressed as an anonymous type, where the properties of the type correspond to the names of the segment variables they constrain. Here we have used a constraint with a regular expression that matches URLs only where the value of the controller variable begins with the letter H.
Note: Default values of route are applied before constraints are checked. So, for example,
if I request the URL /, the default value for controller, which is Home, is applied.
The constraints are then checked, and since the controller value begins with H, the default URL will match the route.
4. Constraining a Route to a Specific Set of Segment Variable Values in the RouteConfig.
Regular expressions can constrain a route so that only specific values for a URL segment will cause a match
namespace UrlsAndRoutes { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { controller = "^H.*", action = "^Index$|^About$" }, new[] { "URLsAndRoutes.Controllers" }); } } }
This constraint will allow the route to match only URLs where the value of the action segment is Index or About. Constraints are applied together, so the restrictions imposed on the value of the action variable are combined with those imposed on the controller variable. This means that the above route will match URLs only when the controller variable begins with the letter H and the action variable is Index or About.
5. Constraining a Route Using HTTP Methods
Routes can be constrained so that they match a URL only when it is requested using a specific HTTP method.
namespace UrlsAndRoutes { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { controller = "^H.*", action = "Index|About", httpMethod = new HttpMethodConstraint("GET") }, new[] { "URLsAndRoutes.Controllers" }); } } }
NOTE: The format for specifying an HTTP method constraint is slightly odd. It does not matter what name is given to
the property(eg. httpMethod), as long as it is assigned to an instance of the HttpMethodConstraint class. In the listing, we called the
constraint property httpMethod to help distinguish it from the value-based constraints we defined previously.
Note: The ability to constrain routes by HTTP method is unrelated to the ability to restrict action methods using attributes such as HttpGet and HttpPost. The route constraints are processed much earlier in the request pipeline, and they determine the name of the controller and action required to process a request. The action method attributes are used to determine which specific action method will be used to service a request by the controller.
Multiple HTTP Methods can be specified by comma separated values.
httpMethod = new HttpMethodConstraint("GET", "POST") },
...
6. Using Type and Value Constraints for Routes
The MVC Framework contains a number of built-in constraints that can be used to restrict the URLs that a route matches based on the type and value of segment variables.
using System.Web.Mvc.Routing.Constraints; namespace UrlsAndRoutes { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { controller = "^H.*", action = "Index|About", httpMethod = new HttpMethodConstraint("GET"), id = new RangeRouteConstraint(10, 20) }, new[] { "URLsAndRoutes.Controllers" }); } } }
In the constraint classes, which are in the System.Web.Mvc.Routing.Constraints namespace, check to see if segment variables are values for different C# types and can perform basic checks. In the listing, I have used the RangeRouteConstraint class, which checks that the value provided for a segment variable is a valid int value that falls between two bounds – , in this case 10 and 20.
The route constraint classes Name Description Attribute Constraint AlphaRouteConstraint() Matches alphabet characters, irrespective of case alpha (A–Z, a–z) BoolRouteConstraint() Matches a value that can be parsed into a bool bool DateTimeRouteConstraint() Matches a value that can be parsed into a DateTime datetime DecimalRouteConstraint() Matches a value that can be parsed into a decimal decimal DoubleRouteConstraint() Matches a value that can be parsed into a double double FloatRouteConstraint() Matches a value that can be parsed into a float float IntRouteConstraint() Matches a value that can be parsed into an int int
LengthRouteConstraint(len) Matches a value with the specified number of characters length(len) LengthRouteConstraint(min, max) or that is between min and max characters in length. length(min, max) LongRouteConstraint() Matches a value that can be parsed into a long long MaxRouteConstraint(val) Matches an int value if the value is less than val max(val) MaxLengthRouteConstraint(len) Matches a string with no more than len characters maxlength(len) MinRouteConstraint(val) Matches an int value if the value is more than val min(val) MinLengthRouteConstraint(len) Matches a string with at least len characters minlength(len) RangeRouteConstraint(min, max) Matches an int value if the value is between min and max range(min, max)
We can combine different constraints for a single segment variable by using the CompoundRouteConstraint class, which accepts an array of
constraints as its constructor argument. In below snippet see how we have used this feature to apply both the AlphaRouteConstraint and
the MinLengthRouteConstraint to the id segment variable to ensure that the route will only match string values that contain solely
alphabetic characters and have at least six characters.
namespace UrlsAndRoutes { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { controller = "^H.*", action = "Index|About", httpMethod = new HttpMethodConstraint("GET"), id = new CompoundRouteConstraint(new IRouteConstraint[] { new AlphaRouteConstraint(), new MinLengthRouteConstraint(6) }) }, new[] { "URLsAndRoutes.Controllers" }); } } }
0 comments:
Post a Comment