nitinvertigo
nitinvertigo

Reputation: 1180

how to display values from database into the layout page in mvc

Here i am trying to make a dynamic and editable layout page in mvc.In my layout page i have a logo( which is presently hard coded). I am implementing a function so that any user can change that logo by uploading his own logo.The path of that logo is stored in database. For example if this is my logo now :

<img alt="" src="~/Images/W_logo.png" style="border-style: none;" width="115px" height="60px" />

I want it to be something like :

<img alt="" src="@item.logo" style="border-style: none;" width="115px" height="60px" />

where 'logo' is a parameter i pass in my model.

or

 <img alt="" src="@ViewData["logopath']" style="border-style: none;" width="115px" height="60px" />

Also if i am using a controller to store the value how can i make it so that it runs everytime the application starts. For example is this is my controller:

public ActionResult layoutChange()
{
  --recover path from database--
  ViewData["logopath"]=path from the database;
  return View(); -- Here the view is the layout page
}

Any help would be greatly appreciated. Thank you.

Upvotes: 2

Views: 1615

Answers (1)

David Tansey
David Tansey

Reputation: 6023

I recommend you try to use Html.RenderAction in your _Layout.cshtml page. You can use this to specify a controller action to invoke.

The controller action can then 'recover path from database' as you indicated, and pass this path to a partial view that contains the markup for your img tag. The result of all of that will become part of the dynamic markup for your _Layout page.

By taking this approach, you don't need to pass the path value to the _Layout page. What happens is the _Layout page calls the controller (via Html.RenderAction), and the controller figures out the path and returns the entire IMG tag markup.

Hope that helps.

Upvotes: 3

Related Questions