Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters in partial Views - MVC3/Razor

How can I pass parameters to a partial view in MVC3 (razor). I replaced a regular View page with a Partial View in my MVC project. For a regular View page, I passed parameters like

   public ActionResult MeanQ(int id)
    {            
        Access access= db.Access.Find(id);
        return View(access);
    }

Now since I changed the view to a partial view, I have the following code instead:

  public ActionResult MeanQ(int id)
    {            
        Access access= db.Access.Find(id);
        return PartialView("_MeanQPartial");
    }

but do not know how I can still pass the parameter 'id' to make it work like before. Please help. For what its worth, the View or the partial View , both are triggered by a link and displayed in a Jquery Modal Dialog box.

like image 726
ZVenue Avatar asked Jun 09 '11 15:06

ZVenue


2 Answers

Try this

return PartialView("PartialViewName", access);
like image 171
Rajesh Avatar answered Oct 17 '22 16:10

Rajesh


Simply give it as 2nd parameter. PartialView method has 4 overloads and this includes one with two parameters PartialView(string viewName, object model)

public ActionResult MeanQ(int id)
{            
    Access access= db.Access.Find(id);
    return PartialView("_MeanQPartial", access);
}

For what its worth, the View or the partial View , both are triggered by a link and displayed in a Jquery Modal Dialog box.

View would return an entire page using your layout. PartialView only returns the HTML from your partial. For a modal dialog, the partial is enough. No need to retrieve a complete page.

like image 36
DanielB Avatar answered Oct 17 '22 18:10

DanielB