English 中文(简体)
我如何把呼吁推向“观点”或“观点”?
原标题:How do I encapsulate calls to ViewBag or ViewData?

我想能够登记在“观点”或“观点Bag”中的文字区块,然后用正确的位置将其上载在我的布局上。

我尝试在我的<代码>_Layout上写一个@Function{},但我的意见不能说。

So how do I write a class that allows me to do something similar to

@Something.registerscript("myscript.js")

之后,在拉伊特网页上

@Something.RenderScripts()

我在这里看到了使用单一吨位模式实施的情况。

Add CSS or JavaScript files to layout head from views or partial views

但是,不担心这将造成问题,因为这样做应该是动态的,而不是静态的!

问题回答

这是否是一种通用的解决办法,或者你只是想把文字档案列入你们的看法? 对于后者来说,你总是可以设立一个称为“Head”的章节,或者在<head>-element中设立的任何部分。

例如:

_layout.cshtml
<html>
     <head>
          @RenderSection("Head", false)
     </head>
     ...
</html>

View.cshtml
@section Head
{
    <script type="text/javascript">...</script>
}

我只想提出这一答案,因为你确实想做的比这还要多(我知道,这只是与编辑部合作等等)。 但是,如果你想复杂,这项工作非常容易。

为什么你们会利用“观点”来做到这一点? 如果你想制造一个具有请求范围的内容,则使用HttpContext。 现任。 物品——在你的情况下采用一种储存器方法,一种“储存一切”方法。

Viewbag or it s close relative viewdata I d say are the best collections to use for this, as your context appears to be within the view. Where would you register the script & where would you render it?

我认为,你可以撰写帮助延期文件,以总结登记和登记;使事情成为一面。 我认为,帮助者的方法可以查阅数据。 如果你只从这些助手方法中提一下,那么你会从你使用的实际储存中获得更多的抽象红利。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签