1

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.

1 Answer 1

1

Do you have any other routes that come before the one we see here? The first route that matches wins.

Sign up to request clarification or add additional context in comments.

4 Comments

i have another route, but it's after this one
The constraint regex maybe? Try @"[A-Z]"
Great. That solved that problem. Now I need to make it so that when a user clicks on the letter, it shows the usernames starting with that letter. I posted my controller code (see above).
Something like this maybe: logic.GetContacts().Where(c => c.UserName.StartsWith(alpha)).Skip(0).Take(20).ToList()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.