Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@model for _layout.cshtml on MVC4?

I was wondering if there's a way to specify a model for the _layout.cshtml file, i've seen lots of posts with the basic same question with people replying with "alternative" solutions, not saying it's not possible nor showing how exactly we could achieve this

having some experience with webforms I've been trying to migrate to MVC and often find myself with such questions, I've found this website: http://blog.bitdiff.com/2012/05/sharing-common-view-model-data-in.html which partially solved my problem but even them don't bind their _layout.cshtml with a @model, as far as I know, I have to specify a model on each view if I want to access the SharedContext, please correct if I'm wrong

what I wanted to do is declare a "@model Namespace.MyModel" on _layout.cshtml so it could retrieve its information by itself, instead of having to implement a model for each view inherinting from the LayoutModel

*I hope I'm being clear, basically, I wanted to know how can I declare @model tag on a _layout.cshtml so it can access its own model

with the solution I linked before (even though it's not linked to my question) I have to do: @(((BaseController)ViewContext.Controller).Context.Property) to get the shared information, and if I could simply declare (and use) a @model instead, I could accomplish the same thing by doing something like: @Model.Property*

as you can see, im struggling trying to migrate whatever I already know from webforms to MVC and it's being quite difficult for me since I have to adopt certain practices which are completely different from what I'm used to

thanks in advance

like image 425
leandro koiti Avatar asked Jul 25 '13 21:07

leandro koiti


People also ask

What is @model in Cshtml?

The @model directive allows access to the list of movies that the controller passed to the view by using a Model object that's strongly typed. For example, in the Index.cshtml view, the code loops through the movies with a foreach statement over the strongly typed Model object: CSHTML Copy.

What are the elements included in the _layout Cshtml?

cshtml . As you can see, the layout view contains HTML Doctype, head, and body tags. The only difference is a call to RenderBody() and RenderSection() methods. The child views will be displayed where the RenderBody() is called.

What is the use of _layout Cshtml in MVC?

The file MasterLayout. cshtml represents the layout of each page in the application. Right-click on the Shared folder in the Solution Explorer, then go to Add item and click View. Copy the following layout code.

How do I view Cshtml in my browser?

Right click the Index. cshtml file and select View in Browser. You can also right click the Index. cshtml file and select View in Page Inspector.


2 Answers

You should delegate the parts of your layout that "need a model" to a separate controller using partial views and RenderAction:

@Html.RenderAction("SomeAction", "LayoutController")

Have LayoutController.SomeAction return a PartialViewResult, which you can then strongly type to a model.

like image 183
Ant P Avatar answered Oct 19 '22 18:10

Ant P


Even though you already accepted an answer, based on your saying you are just pulling an image URL you should do it using JQuery, not a model.

This code is untested, apologies for that. Feel free to point out if I typed a bug. The HTML element containing the background image has the id="url" attribute so the selectors work.

Controller

[HttpGet]
public string GetSessionUrl()
{
    //logic to detmine url
    return url;
}

JQuery

$(document).ready(function () {
    var $url = $('#url');
    var options = {
        url: "/Home/GetSessionUrl",
        type: "get",
        async:false
    };

    $.ajax(options).done(function (data) {
        $url.attr('src', data);
    });
});
like image 27
Lotok Avatar answered Oct 19 '22 18:10

Lotok