views:

73

answers:

1

I am basically taking the default ASP.NET MVC template and extending it:

Looking at the site.master, I see this for menus:

<ul id="menu">              
    <li><%= Html.ActionLink("Home", "Index", "Home")%></li>
     <li><%= Html.ActionLink("About", "About", "Home")%></li>
</ul>

I am then editing it by the following:

<ul id="menu">              
    <li><%= Html.ActionLink("Home", "Index", "Home")%></li>
    <li><%= Html.ActionLink("About", "About", "Home")%></li>
    <li><%= Html.ActionLink("My Contact Info", "Details/" + Html.Encode(Page.User.Identity.Name), "Users")%></li>
</ul>

The issue is that, in the controller, I have two different methods:

//
// GET: /Users/Details/userName
public ActionResult Details(string loginName)
{
    UserInfo user = repo.GetUserByName(loginName);
    return View(user);
}

//
// GET: /Users/Details/5
public ActionResult Details(int id)
{
    UserInfo user = repo.GetUser(id);
    return View(user);
}

I get an exception that says, ambiguous method. Because it can't figure out which one to call. I would hope that it could figure it out by the data type but apparently not.

Does anyone have any suggestions for supporting details view and querying by different fields? (Of course all of the queryable fields are unique.)

+2  A: 

If you had a route like:

routes.MapRoute(
            "Users",                                              // Route name
            "Users/Details/{username}",                           // URL with parameters
            new { controller = "Users", action = "Details", username = "" }  // Parameter defaults
        );

which could be used to avoid ambiguity with the default route .. You would need to put this before the default "{controller}/{action}/{id}" route so that it would get priority...

Then, you could have an action like :

// GET: /Users/Details/username   or //GET: /Users/Details?id=theId
public ActionResult Details(string username, int? id)
{
   if(id!=null)
   {
      UserInfo user = repo.GetUser(id.Value);
      return View(user);
   }
   if(!String.IsNullOrEmpty(username))
   {
      UserInfo user = repo.GetUserByName(username);
      return View(user);
   }
...//Handle bad url.. redirect to error...you decide ;) 
}

You could then access this action like this:
/Users/Details/username or..
/Users/Details?id=userId

markt
can you explain here or give an example given the code above. i dont quite understand
ooo
Modified the answer a bit -- this is one way to do what I think you are looking for...
markt