@Model的_layout.cshtml上MVC4? [英] @model for _layout.cshtml on MVC4?

查看:166
本文介绍了@Model的_layout.cshtml上MVC4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有一种方法来指定型号为_layout.cshtml文件,我见过很多职位与人的回答与另类的解决方案,不是说这是不可能的,也没有说明如何在基本相同的问题正是我们能做到这一点。

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

具有web表单我一直在试图迁移到MVC和经常发现自己有这些问题的一些经验,我发现这个网站:<一href="http://blog.bitdiff.com/2012/05/sharing-common-view-model-data-in.html">http://blog.bitdiff.com/2012/05/sharing-common-view-model-data-in.html 这部分地解决我的问题,但即使他们不自己_layout.cshtml绑定一个@model,据我知道,我必须指定每个视图模型,如果我想要访问SharedContext,请纠正,如果我错

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

我想要做的是申报了@model Namespace.MyModel上_layout.cshtml,因此它可以自行恢复,而不必实施模型为每个视图从LayoutModel

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

*我希望我是清楚的,基本上,我想知道我怎么可以宣布在_layout.cshtml @model标记,以便它可以访问自己的模式

*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

与解决方案,我之前(即使它没有链接到我的问题),我必须做的链接: @(((BaseController)ViewContext.Controller).Context.Property)来获取共享信息,如果我可以简单地声明(和使用)一个@model相反,我可能会做类似的东西完成同样的事情:@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*

你可以看到,即时通讯挣扎着试图迁移不管我从web表单到MVC已经知道,它的存在对我来说很困难,因为我不得不采取这是从我已经习惯了完全不同的某些做法

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

在此先感谢

推荐答案

即使你已经接受了答案,根​​据你说你只是拉着你应该使用JQuery,而不是一个模型,做一个图像URL。

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.

这code是未经检验的,道歉的。随意地指出,如果我输入一个错误。包含背景图片的HTML元素的 ID =URL属性,这样的选择工作。

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.

控制器

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

JQuery的

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);
    });
});

这篇关于@Model的_layout.cshtml上MVC4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆