Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass parameters to partial view to show db content?

Tags:

asp.net-mvc

I am getting my content from a database. How can i use partial views to show content on page using that database?
database table: Content
[Id, Content] these are 2 fields

i want to get the content from db using partial views.
How i will pass an id to a partial view and show the content in view page?

like image 898
coure2011 Avatar asked Mar 15 '10 14:03

coure2011


1 Answers

You could use Html.RenderAction:

public class MyController 
{
    [ChildActionOnly]
    public ActionResult Foo(int id) 
    {
        var content = GetContentFromDatabase(id);
        return Content(content, MediaTypeNames.Text.Html);
    }
}

And in your view include the partial:

<%= Html.RenderAction("foo", "mycontroller", new { id = 5 }) %>

Remark: RenderAction is part of the now released ASP.NET MVC 2 RTM. For ASP.NET MVC 1 you may take a look at the Futures assembly containing this extension method.

like image 72
Darin Dimitrov Avatar answered Oct 16 '22 02:10

Darin Dimitrov