简体   繁体   English

在MVC视图之间传递参数以匹配模型中可用数据源中的项

[英]Passing arguments between MVC views to match an item from a data source available in the model

I have a view that renders a side bar for a content page. 我有一个呈现内容页面侧栏的视图。 The side bar can be rendered several times on a single page. 侧栏可以在单个页面上渲染多次。 The page's layout defines several content sections (or, positions , in Kooboo's terminology) which are, in an actual page, bound to specific views. 页面的布局定义了几个内容区段(或位置 ,在Kooboo的术语),这是,在实际的页,绑定到特定视图。 In each such view I need to optionally render a side bar by redering the general-purpose side bar view. 在每个此类视图中,我都需要通过重新设置通用侧栏视图来选择性地渲染侧栏。

Side bars' content is stored in Kooboos database. 侧栏的内容存储在Kooboos数据库中。 I'm using a specific content type which has, for the sake of simplicity, the following two fields: 我正在使用一种特定的内容类型,为简单起见,它具有以下两个字段:

  • Position — the ID of the position in the page, which matches an ID of a position defined in the layout Position —页面中位置的ID,与布局中定义的位置的ID相匹配
  • Content — an HTML fragment that constitute's the side bar's actual content Content -构成侧边栏实际内容的HTML片段

The side bar view's logic should be as follows: from all side bar data bound to the current page select a single item with a Position matching a position passed from the outer view as an argument , and, if found, render it. 侧栏视图的逻辑应如下:从绑定到当前页面的所有侧栏数据中,选择一个Position与从外部视图传递的Position相匹配的位置作为参数的单个项目,如果找到,则将其渲染。 The problem is, I can't figure out a way to pass arguments into a view 问题是,我想不出一种将参数传递给视图的方法

Sample layout fragment: 示例布局片段:

<!-- Header section -->
<section>
    <div class="section-content">
        @Html.FrontHtml().Position("Header")
        @Html.FrontHtml().Position("HeaderSectionContent")
    </div>
</section>

<!-- Main section -->
<section class="even">
    <div class="section-content">
        @Html.FrontHtml().Position("MainSectionContent")
    </div>
</section>

Sample view bound to the Header position: 绑定到Header位置的示例视图:

<div class="header-container">
    <h1 class="page-title" @ViewHelper.Edit(ViewBag.Solution, "Title")>
        @ViewBag.Solution.Title
    </h1>

    <!-- How to pass "Header" string as an argument into the Controls.Sidebar view? -->
    @Html.FrontHtml().RenderView("Controls.Sidebar",ViewData)

    <h2 @ViewHelper.Edit(ViewBag.Solution, "Perex")>
        @Html.Raw(ViewBag.Solution.Perex)
    </h2>
</div>

The intended general purpose side bar view Controls.Sidebar : 预期的通用侧栏视图Controls.Sidebar

@if (ViewBag.Sidebars != null) 
{
    // How to retrieve an argument and select the matching side bar data?
    var sidebar = ViewBag.Sidebars ... ;
    if (sidebar != null)
    {
        <div class="side-bar">
            <h3>@item.Title</h3>
            @Html.Raw(item.Content)
        </div>
    }   
}

At the end of the day I need to be able to render this view several times, and conditionally render several side bars in respect to data availability. 最终,我需要能够多次渲染该视图,并有条件地渲染几个有关数据可用性的侧栏。

I was looking into the Parameters definitions available in the view editor, but it seems this provides access to query string parameters, which is not a mechanism I could (would like to) leverage. 我一直在研究视图编辑器中可用的Parameters定义,但似乎这提供了对查询字符串参数的访问,这不是我可以(希望)利用的机制。 Kooboo documentation is lacking any information related to this topic. Kooboo文档缺少与此主题相关的任何信息。

There are at least two ways to pass some parameters from one view into another in Kooboo: 在Kooboo中,至少有两种方法可以将某些参数从一个视图传递到另一个视图:

First you can store your parameter value in the parent view like this: 首先,您可以将参数值存储在父视图中,如下所示:

    Context.Items["ParameterName"]=value;

or like this: 或像这样:

    var customViewData = new ViewDataDictionary();
    customViewData.Add("ParameterName", value);

Then you can call rendering of a child view: 然后,您可以调用子视图的渲染:

    @Html.FrontHtml().RenderView("ChildViewName", ViewData) //a default ViewData passed

or as following, respectively: 或分别如下:

    @Html.FrontHtml().RenderView("ChildViewName", customViewData)

On the receiving side in ChildViewName you simply fetch the parameter value either thus: 在ChildViewName的接收端,您只需获取参数值即可:

    var value = Context.Items["ParameterName"];

or thus, respectively: 或因此分别:

    var value = ViewData["ParameterName"]; // NB: don't forget to cast parameter to a proper data-type, because ViewData is not processed same smoothly as ViewBag would. 

Both ways work just fine for this purpose. 两种方法都可以达到此目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM