@model for _layout.cshtml on MVC4?

I was wondering if there is a way to specify a model for the _layout.cshtml file, I saw a lot of posts with the same question with people answering โ€œalternativeโ€ solutions, not saying that this is impossible, and showing how exactly we could achieve of this

having some experience with web forms that I tried to port to MVC and often come across such questions, I found this site: http://blog.bitdiff.com/2012/05/sharing-common-view-model- data-in.html that partially solved my problem, but even they donโ€™t associate their _layout.cshtml with @model, as far as I know, I have to specify a model for each view, if I want to access the SharedContext, please correct if I wrong

what I wanted to do was declare "@model Namespace.MyModel" on _layout.cshtml so that it could get its information on its own, instead of implementing a model for each view inheriting from LayoutModel

* I hope itโ€™s clear to me basically, I wanted to know how I can declare the @model tag on _layout.cshtml so that it can access its own model.

with the solution I linked earlier (although this is not related to my question), I have to do: @ (((BaseController) ViewContext.Controller) .Context.Property) to get general information, and if I could just declare ( and use) @model instead, I could do the same by doing something like: @Model. Property *

as you can see, I am trying to transfer everything that I already know from webforms to MVC, and it is quite difficult for me, since I have to accept certain methods that are completely different from what I used for

early

+6
source share
4 answers

Despite the fact that you have already accepted the answer, based on your statement, you simply pull the URL of the image that you have to make using jQuery, not the model.

This code is not verified, apologized for it. Feel free to indicate if I typed an error. 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); }); }); 
+5
source

You must delegate 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 can then be heavily printed in the model.

+6
source

You can add BaseModel to _Layout.

 @model BaseModel 

Then all models are inherited from this BaseModel class.

 public class MyModel : BaseModel { } 

As others have stated, this is not a good practice. If your model forgets to inherit from BaseModel , it will throw an exception at runtime. However, it is up to you.

+3
source

In BaseController, you can declare any model as a property.

 public class BaseController : Controller { public BaseController () { MyTag = new TagModel (); // or get db, take any value from there } public TagModel MyTag { get; set; } } 

In action:

 ViewBag.MyTag = MyTag ; 

And in _Layout.cshtml you can use

 @{ var myTag = (TagModel)ViewBag.MyTag; } 
+2
source

Source: https://habr.com/ru/post/950306/


All Articles