Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you need to put some default values into your route for the post title... and make sure they map through. You don't seem to have default values for 'postId' and 'title' and yet you have one for a non-existant 'post' route value.</p> <pre><code>routes.MapRoute( "PostDetails", "posts/{postId}/{title}", new { controller = "Posts", action = "Details", postId = 0, title = "" }, new { PostId = @"\d+", Title = @"([A-Za-z0-9-]+)" } ); </code></pre> <p>Posts controller</p> <pre><code>public ActionResult Details(int postId, string title) { //whatever } </code></pre> <p>Then in your view</p> <pre><code>&lt;%= Html.ActionLink(Model.Title, "Details", new { @postId = Model.PostID, @title = Model.Title }) %&gt; </code></pre> <p>Or</p> <pre><code>&lt;%= Html.ActionLink(Model.Title, "Details", "Posts", new { @postId = Model.PostID, @title = Model.Title }, null) %&gt; </code></pre> <p>I would also suggest creating a TitleSlug property on your posts model.</p> <p>E.g. (code taken from <a href="http://www.intrepidstudios.com/blog/2009/2/10/function-to-generate-a-url-friendly-string.aspx" rel="nofollow noreferrer">here</a>)</p> <pre><code>public partial class Post { public string TitleSlug { get { string str = Title.ToLower(); str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); // invalid chars str = Regex.Replace(str, @"\s+", " ").Trim(); // convert multiple spaces into one space str = str.Substring(0, str.Length &lt;= 45 ? str.Length : 45).Trim(); // cut and trim it str = Regex.Replace(str, @"\s", "-"); // hyphens return str; } } } </code></pre> <p>HTHs<br> Charles</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload