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

我想知道,如果可能的话,什么是使用新的剃须刀视图引擎渲染部分的最佳方法。我知道这件事还没有完全结束

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

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

调用 RenderPage 的页面使用定义了三个部分的布局(母版)页面: TitleContent、 HeadContent 和 Maincontent。当我试图从这个页面呈现我的区域设置控件时,似乎这些部分也是必需的——它们应该只需要在调用页面中并且存在。无论我是否在部分视图中包含这些节,我都会收到以下消息(显然我不想包含这些节,但这似乎是一个有趣的调试点...)。

以下部分是 已定义但尚未在 布局页面 “ ~/Views/Shared/LocaleUserControl.cshtml”: 标题内容; 标题内容; 主要内容

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

@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>
104931 次浏览

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)

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.