的Razor视图引擎 - 我如何添加局部视图 [英] Razor view engine - How can I add Partial Views

查看:87
本文介绍了的Razor视图引擎 - 我如何添加局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果有可能,是使用新的Razor视图引擎渲染部分的最佳方式。我明白这是一件没有被时间彻底完蛋了。

现在我用RenderPage来呈现用户控件:

  @RenderPage(〜/查看/共享/ LocaleUserControl.cshtml,ViewData.Model)

调用RenderPage页面使用带有定义了三个部分的布局(主)页:TitleContent,HeadContent和日程地址搜索Maincontent。当我尝试从这个网页它看来,这些部分也需要使我的区域控制 - 他们只应在调用页面需要,并present。我收到以下消息,不管我是否不包括在我的局部视图的部分(显然我不想包括这些部分,但它似乎是一个有趣的调试点...)。


  

下面的部分已
  定义,但尚未呈现上
  布局页面
  〜/查看/共享/ LocaleUserControl.cshtml:
  TitleContent; HeadContent;日程地址搜索Maincontent


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

  @inherits System.Web.Mvc.WebViewPage< LocaleBaseModel>
@using System.Web.UI程序;&所述p为H.;
     @ Html.LabelFor(型号=> Model.CountryName)
    < BR />
    @ Html.DropDownListFor(型号=> Model.CountryName,空的String.Empty,新{@class =TEXT,ACCESSKEY =U})
&所述; / P>
&所述p为H.;
     @ Html.LabelFor(型号=> Model.StateProvince)
    < BR />
     @ Html.DropDownListFor(型号=> Model.StateProvince,空的String.Empty,新{@class =TEXT,ACCESSKEY =T})
&所述; / P>
<脚本类型=文/ JavaScript的>
    $(函数(){
        VAR国家= $(#国家或地区名称);
        VAR statesprovinces = $(#StateProvince);
        countries.change(函数(){
            。statesprovinces.find('选项')删除();
            VAR URL ='@ Url.Action(GetStatesProvinces,基地)';
            $ .getJSON(URL,{countryId:countries.val()},功能(数据){
                $(数据)。每个(函数(){
                    $(<期权价值=+ this.ID +>中+ this.Name +< /选项>中)。appendTo(statesprovinces);
                });
            });
        });
    });
< / SCRIPT>


解决方案

您的部分看起来像一个编辑模板,所以你可以把它作为这样的(假设,当然,你的部分被放置在〜 /视图/ controllername / EditorTemplates 子文件夹):

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

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

  @ Html.Partial(nameOfPartial模型)

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

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

@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...).

The following sections have been defined but have not been rendered on the layout page '~/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>

解决方案

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):

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

Or if this is not the case simply:

@Html.Partial("nameOfPartial", Model)

这篇关于的Razor视图引擎 - 我如何添加局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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