views:

57

answers:

1

I have an action link in one of my view page

<%=Html.ActionLink("Details", "Details", new { id = Model.Id })%> and redirects me to page which has a url like this http://localhost:1985/Materials/Details/2 instead of this i would like to have my url as http://localhost:1985/Materials/Details/steel material name instead of Id... Is this possible...... This is my controller action method,

    public ActionResult Details(int id)
    {
        var material = consRepository.GetMaterial(id);
        return View("Details", material);
    }

EDIT: I am iterating my json object returned from a jsonresult controller....

$.each(data.Results, function() {
            divs += '<a href="/Materials/Details/' + this.Id + '">Details</a>
            &nbsp;<a href="/Materials/Edit/' + this.Id + '">Edit</a></div>';
            });

My route look like this,

routes.MapRoute(
                "Default",                                           
                "{controller}/{action}/{id}",                         
                new { controller = "Materials", action = "Index", id = "" } 
            );
+2  A: 

I would recommend that your URL be

http://localhost:1985/Materials/Details/2/Steel

This seems to be the way that SO displays their URL's as well.

Your routes would be defined as

routes.MapRoute(
  "action with slug", 
  "{controller}/{action}/{id}/{slug}",
  new {controller = "Error", action = "NotFound", id = "", slug = ""}
);

routes.MapRoute(
  "Default",                                           
  "{controller}/{action}/{id}",                         
  new { controller = "Materials", action = "Index", id = "" } 
);

In answer to 2nd question in comments, "How do you remove Details from link?" If you want to remove the ACTION name, so the URL is just http://site/controler/id/slug, add the following route BEFORE the action with slug route.

routes.MapRoute(
  "controller with slug", 
  "Materials/{id}/{slug}",
  new {controller = "Materials", action = "Details", id = "", slug = ""}
);

The 'slug' which would capture the word steel, would be ignored by the action, because you only want the Id to retrieve the material anyway.

Create a route link instead of an action link, like this

<%= Html.RouteLink(material.Name,
      "show with slug", 
      new { controller = "Materials", 
            action = "Details", 
            id = material.Id, 
            slug = Server.HtmlDecode(material.Name).Replace(" ","-")
      }) 
%>

I replace spaces in my 'slug's with hyphens so they are not replaced by %20 by the browser.

Your Details ActionResult will remain the same.

Jason Watts
@jason will definitely try this....
Pandiya Chendur
@jason i am iterating json data with jquery and adding the details link via jquery..... Here is what i use `<a href="/Materials/Details/' + this.Id + '/' + this.Mat_Name + '">Details</a>` but this doesn't seem to work...
Pandiya Chendur
@jason but if i use `<a href="/Materials/Details/' + this.Id + '">Details</a> it works and i am redirected `http://localhost:1985/Materials/Details/2`
Pandiya Chendur
What do your route mappings look like? The order of them could be the issue.
Jason Watts
@jason look at my edit now...
Pandiya Chendur
@jason it worked just changed the route...
Pandiya Chendur
@jason how to strip details from the route?
Pandiya Chendur
Shouldn't that be a new question?? :) I'll post another edit..
Jason Watts
@jason ya it should but somehow it relates to this quertion as well..
Pandiya Chendur
Does that additional route give you what you need?
Jason Watts
BTW, I think your routes are backwards. `Default` route will always match before `action with slug` route.
R0MANARMY
You are correct, I added them in the order asked. Will modify for future clarity :) Thanks.
Jason Watts