Reputation: 1
I am having following table in my database.
Userid username usercity
1 amit mumbai
2 vishal ahmedabad
3 shekhar indore
4 nitin bhopal
5 dhanraj jaipur
I already created viewmodel & controller and displayed this using DBML.
http://example.com/Home/UserList
but when i click on details link, url structure as follows:
http://example.com/mymvc1/home/details/5
5
is the ID.
I don't want to pass Id, I want to pass username
Id should be automatically converted into username.
How can I achieve this?
Upvotes: 0
Views: 51
Reputation: 9298
Add {username}
to route instead of id kinda like:
routes.MapRoute("ViewUser", "Somepath/View/{username}",
new { controller = "Users", action = "View" }
);
and then in your listning use like
@Html.ActionLink("View User", "View", new { username = item.Username })
And then in your controller have:
public ActionResult View(string username)
UPDATE:
Note that the above is only a good idea if username is unique. Else you can do username as an optional parameter so you get the name in the url.
Like this:
routes.MapRoute("ViewUser", "Somepath/View/{userID}/{username}",
new { controller = "Users", action = "View", username = UrlParameter.Optional },
new { userID = @"\d+" }
);
And then user
@Html.ActionLink("View User", "View", new { userID = item.UserID, username = item.Username })
And back to int userID
in the controller
Upvotes: 4