I have a route
routes.MapRoute(
"AlphaPagedContacts", // Route name
"Contact/Alpha{alpha}", // URL with parameters
new { controller = "Contact", action = "AlphaList", alpha = UrlParameter.Optional },
new { alpha = @"\A-Z" } // Parameter defaults
);
I’m trying to make the URL’s display like
/Contact/AlphaA
/Contact/AlphaB
for contacts based on username. However, the URL’s are showing up as: http://localhost:54568/Contact/AlphaList?alpha=H
My HTMLHelper is
@Html.AlphaLinks(new PagingModel { MaxPages = Model.MaxPages, CurrentLetter = Model.CurrentLetter, UrlGeneratorFunctionAlpha = i => Url.Action("AlphaList", new { alpha = i }) })
and my implementation is
public static MvcHtmlString AlphaLinks(this HtmlHelper helper, PagingModel model)
{
string[] letters = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
var stringBuilder = new StringBuilder("<ul class='pager'>");
foreach (string letter in letters)
{
stringBuilder.Append(String.Format("<li {2}><a href='{1}'>{0}<a></li>", letter, model.UrlGeneratorFunctionAlpha(letter), letter == model.CurrentLetter ? "class=Selected" : String.Empty));
}
stringBuilder.Append("</ul>");
return MvcHtmlString.Create(stringBuilder.ToString());
}
My controller code for the actionresult is:
public ActionResult AlphaList(string alpha = "A")
{
var logic = new ContactBUS();
var pageSize = 10;
var usernames = from c in XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Contacts.xml")).Elements("Contact")
select new
{
Username = (string)c.Element("Username"),
Lastname = (string)c.Element("LastName"),
Firstname = (string)c.Element("FirstName"),
Email = (string)c.Element("Email"),
};
var model = new AlphaListContactViewModel
{
Contacts = logic.GetContacts().Skip(0).Take(20).ToList(),
CurrentLetter = alpha,
MaxPages = (int)Math.Ceiling(logic.GetContactsCount() / (double)pageSize)
};
return View(model);
}
Any idea why this is happening?
Also, I need to make it so that when the user clicks on a letter, it shows the contacts’ usernames for that letter.
Any help would be greatly appreciated.
Do you have any other routes that come before the one we see here? The first route that matches wins.