Overloading Controller Actions
Posted
by DaveDev
on Stack Overflow
See other posts from Stack Overflow
or by DaveDev
Published on 2010-03-12T15:34:41Z
Indexed on
2010/03/12
15:37 UTC
Read the original article
Hit count: 242
Hi Guys
I was a bit surprised a few minutes ago when I tried to overload an Action in one of my Controllers
I had
public ActionResult Get()
{
return PartialView(/*return all things*/);
}
I added
public ActionResult Get(int id)
{
return PartialView(/*return 1 thing*/);
}
.... and all of a sudden neither were working
I fixed the issue by making 'id' nullable and getting rid of the other two methods
public ActionResult Get(int? id)
{
if (id.HasValue)
return PartialView(/*return 1 thing*/);
else
return PartialView(/*return everything*/);
}
and it worked, but my code just got a little bit ugly!
Any comments or suggestions? Do I have to live with this blemish on my Controllers?
Thanks
Dave
© Stack Overflow or respective owner