剃刀视图引擎-如何添加部分视图

时间:2023-01-19 16:12:40

I was wondering what, if it is possible, is the best way to render a partial using the new razor view engine. I understand this is something that wasn't finished completely by the time

我想知道,如果可能的话,使用新的razor视图引擎呈现部分内容的最佳方式是什么。我知道这是当时还没有完全完成的事情

Right now I am using RenderPage to render the user control:

现在我正在使用RenderPage呈现用户控件:

@RenderPage("~/Views/Shared/LocaleUserControl.cshtml",ViewData.Model)

The page calling RenderPage uses a layout (master) page with three sections defined: TitleContent, HeadContent and Maincontent. When I attempt to render my locale control from this page it appears that these sections are also required - they should only be required in the calling page and are present. I receive the following message, regardless of whether or not I include the sections in my partial view (obviously I dont want to include these sections but it seemed like an interesting debugging point...).

调用RenderPage的页面使用一个布局(master)页面,其中定义了三个部分:TitleContent、HeadContent和Maincontent。当我尝试从这个页面呈现我的locale控件时,看起来这些部分也是必需的——它们只应该在调用页面中被要求并且是存在的。我收到以下消息,不管我是否在我的部分视图中包含这些部分(显然我不想包含这些部分,但它看起来像是一个有趣的调试点…)。

The following sections have been defined but have not been rendered on the layout page '~/Views/Shared/LocaleUserControl.cshtml': TitleContent; HeadContent; MainContent

已定义了以下部分,但未在布局页面的~/Views/Shared/LocaleUserControl上呈现。cshtml”:TitleContent;HeadContent;MainContent

My partial view is as follows (adapted from the following link):

我的部分观点如下(改编自以下链接):

@inherits System.Web.Mvc.WebViewPage<LocaleBaseModel>
@using System.Web.UI;

<p>
     @Html.LabelFor(model => Model.CountryName)
    <br />
    @Html.DropDownListFor(model => Model.CountryName,null, string.Empty, new { @class = "text", accesskey="u"})
</p>
<p>
     @Html.LabelFor(model => Model.StateProvince)
    <br />
     @Html.DropDownListFor(model => Model.StateProvince, null, string.Empty, new { @class = "text", accesskey="t" })
</p>


<script type="text/javascript">
    $(function () {
        var countries = $("#CountryName");
        var statesprovinces = $("#StateProvince");
        countries.change(function () {
            statesprovinces.find('option').remove();
            var url = '@Url.Action("GetStatesProvinces", "Base")';
            $.getJSON(url, { countryId: countries.val() }, function (data) {
                $(data).each(function () {
                    $("<option value=" + this.ID + ">" + this.Name + "</option>").appendTo(statesprovinces);
                });
            });
        });
    });
</script>

2 个解决方案

#1


120  

You partial looks much like an editor template so you could include it as such (assuming of course that your partial is placed in the ~/views/controllername/EditorTemplates subfolder):

您的部分看起来很像编辑器模板,因此您可以包含它(当然,假设您的部分放在~/views/controllername/EditorTemplates子文件夹中):

@Html.EditorFor(model => model.SomePropertyOfTypeLocaleBaseModel)

Or if this is not the case simply:

或者如果不是简单的情况:

@Html.Partial("nameOfPartial", Model)

#2


0  

If you don't want to duplicate code, and like me you just want to show stats, in your view model, you could just pass in the models you want to get data from like so:

如果你不想重复代码,像我一样你只想显示统计数据,在你的视图模型中,你可以传入你想要获取数据的模型,比如:

public class GameViewModel
{
    public virtual Ship Ship { get; set; }
    public virtual GamePlayer GamePlayer { get; set; }     
}

Then, in your controller just run your queries on the respective models, pass them to the view model and return it, example:

然后,在您的控制器中运行您对各自模型的查询,将它们传递给视图模型并返回它,例如:

GameViewModel PlayerStats = new GameViewModel();

GamePlayer currentPlayer = (from c in db.GamePlayer [more queries]).FirstOrDefault();

[code to check if results]

[检查结果的代码]

//pass current player into custom view model
PlayerStats.GamePlayer = currentPlayer;

Like I said, you should only really do this if you want to display stats from the relevant tables, and there's no other part of the CRUD process happening, for security reasons other people have mentioned above.

就像我说的,如果您想要从相关的表中显示统计数据,并且没有发生CRUD过程的其他部分,出于安全原因,其他人已经在上面提到过。

#1


120  

You partial looks much like an editor template so you could include it as such (assuming of course that your partial is placed in the ~/views/controllername/EditorTemplates subfolder):

您的部分看起来很像编辑器模板,因此您可以包含它(当然,假设您的部分放在~/views/controllername/EditorTemplates子文件夹中):

@Html.EditorFor(model => model.SomePropertyOfTypeLocaleBaseModel)

Or if this is not the case simply:

或者如果不是简单的情况:

@Html.Partial("nameOfPartial", Model)

#2


0  

If you don't want to duplicate code, and like me you just want to show stats, in your view model, you could just pass in the models you want to get data from like so:

如果你不想重复代码,像我一样你只想显示统计数据,在你的视图模型中,你可以传入你想要获取数据的模型,比如:

public class GameViewModel
{
    public virtual Ship Ship { get; set; }
    public virtual GamePlayer GamePlayer { get; set; }     
}

Then, in your controller just run your queries on the respective models, pass them to the view model and return it, example:

然后,在您的控制器中运行您对各自模型的查询,将它们传递给视图模型并返回它,例如:

GameViewModel PlayerStats = new GameViewModel();

GamePlayer currentPlayer = (from c in db.GamePlayer [more queries]).FirstOrDefault();

[code to check if results]

[检查结果的代码]

//pass current player into custom view model
PlayerStats.GamePlayer = currentPlayer;

Like I said, you should only really do this if you want to display stats from the relevant tables, and there's no other part of the CRUD process happening, for security reasons other people have mentioned above.

就像我说的,如果您想要从相关的表中显示统计数据,并且没有发生CRUD过程的其他部分,出于安全原因,其他人已经在上面提到过。