user92880
user92880

Reputation: 17

MVC return Action search for current Actionresult's view

  public ActionResult xyz(int? page)
        {
            return Index(page);
        }

What I want to do is xyz return same thing as index. I just want to define two url which is actual same. I check that it's trying to find xyz.cshtml

it's not look good. I thing it will just return index function and it's done. It's amazed me. Do someone can show me here if I return the index function directly then how it can search xyz.cshtml.

Someone please tell me the way so index.cshtml is used and the way I return Index show the page as I want.

Paritosh's answer make return the page that I want. but it's create problem that nothing is passed now through Viewdata and Viewbag.

 public ActionResult Index(int? page)
        {
            int pagenum = page ?? 1, limit = Globals.xyz_PAGE_SIZE;

            int startrow = (pagenum - 1) * limit;

            ViewBag.xyzCount = xyz.xyzget();
            ViewBag.Pagesize = Globals.xyz_PAGE_SIZE;
            ViewBag.xyz= blahblah.xyz(startrow, Globals.xyz_PAGE_SIZE + 1);
            return View();
}

my meaning to create the another action is just create two url. what about if i do it with routing. Is routing is better option or their is no good way to deal with this trouble.

Upvotes: 1

Views: 1205

Answers (2)

Gilles V.
Gilles V.

Reputation: 1404

In fact you have two choices :

//immediately show the "Index" View
return View("<Path_To_Index_View>", page);

Or

//Redirect to the "Index" Action
return RedirectToAction("Index", page);

Choose the one you need.

Upvotes: 2

Paritosh
Paritosh

Reputation: 11568

You need to pass view name here

public ActionResult xyz(int? page)
{
    return View("Index",page);
}

If you don't pass View name, then it'll search view which is having same name as Action method - which is xyz.cshtml here. That's why you were facing issue

Upvotes: 6

Related Questions