grafic.web 0 Newbie Poster

Hi, i did create the following class in my Models folder and i create the Styles controller, everuthing is fine... but muy problem is the following.

When i clicK on the link style i got my list of styles and when i click in one style i got the idin the address bar :

localhost/style/1

i didt set a "Type" and a "SubType" and i would love to see them on my bar address, how can i do?

localhost/style/Type/SubType/1

    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;

    namespace Collectionhair.Models
    {
        public class Style
        {
            public int ID { get; set; }

            [Display(Name = "Date Pubblication")]
            [DataType(DataType.Date)]
            [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
            public DateTime ReleaseDate { get; set; }

            public string Type { get; set; }
            public string SubType { get; set; }

            [StringLength(400, MinimumLength = 3)]
            public string Title { get; set; }
            [StringLength(1000, MinimumLength = 3)]
            public string SubTitle { get; set; }
            public string Body { get; set; }


            public string Photo { get; set; }

        }

        public class db514717629DBContext : DbContext
        {
            public DbSet<Style> db514717629 { get; set; }
        }
    }

Dani AI

Generated

wanted the Type and SubType to appear as URL segments instead of only the numeric id. The change is not in the model — it is in routing and link generation. Two solid approaches are: add a custom conventional route before the default route, or enable attribute routing and decorate the Details action. Also make sure the strings you put into the URL are slug-safe (no spaces or special chars).

Add a route (RouteConfig) that matches the extra segments and put it before the default route:

routes.MapRoute(
    name: "StyleByType",
    url: "Style/{type}/{subtype}/{id}",
    defaults: new { controller = "Style", action = "Details", id = UrlParameter.Optional }
);

Or enable attribute routing and declare the route on the action:

routes.MapMvcAttributeRoutes();

// in StylesController
[Route("Style/{type}/{subtype}/{id:int}")]
public ActionResult Details(string type, string subtype, int id) { ... }

Generate the URLs from the view with route values so MVC fills those segments:

@Html.ActionLink(style.Title, "Details", "Style",
    new { type = style.TypeSlug, subtype = style.SubTypeSlug, id = style.ID }, null)

Create a small slug function (lowercase, replace spaces with hyphens, strip invalid chars) and store or compute TypeSlug/SubTypeSlug so links are safe and SEO-friendly. Keep routes ordered (specific before default), add a constraint like {id:int} to avoid collisions, and consider the id-first pattern Style/{id}/{slug} if you want robustness (lookups use id; slug is just for readability). For attribute routing details and examples see .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.