dtjmsy
dtjmsy

Reputation: 2752

Asp.net mvc 3 - actioncontroller returning partialview another actioncontroller

I would like to have an action controller which return a partialview of another actioncontroller.

public ActionResult Method1 (string s)
{
return PartialView (_PartialViewMethod1, object1);
}

public ActionResult Method2 ()
{
return PartialViewOfMethod1;
}

I tried this on method2: return PartialView (Method1(s)) but it does not work, how can I achieve this ?

Thanks

Upvotes: 1

Views: 1426

Answers (3)

Shyju
Shyju

Reputation: 218722

This should give you the result you are looking for

public ActionResult Method2 ()
{
   return PartialView (_PartialViewMethod1, object1);
}

I guess you have some code which you have in common for both the action methods and that is why you want to call another action, then probably you move that to a common method and call that from wherever you want. Some Reactoring !

public YourViewModel GetData(srting s="")
{
  YourViewModel obj1=new YourVieWModel()
  // set some property values or do some operations to get data
  //your custom code
  return obj1; 
}

public ActionResult Method1 (string s)
{
  return PartialView (_PartialViewMethod1,  GetData(s));
}

public ActionResult Method2 ()
{
   return PartialView (_PartialViewMethod1, GetData());
}

Upvotes: 0

Simon Linder
Simon Linder

Reputation: 3428

Try this:

public ActionResult Method2()
{
    string s = "someDefinedString";

    // instead of return PartialView(Method1(s));
    return Method1(s);
}

Upvotes: 2

Yorgo
Yorgo

Reputation: 2678

public ActionResult Method2 ()
{
return PartialView ("Method1", object1);
}

Upvotes: 0

Related Questions