[英]How can I bind Dictionary to a partial view in ASP.NET MVC 5 Razor template engine
[英]Razor view engine - How can I add Partial Views
我想知道,如果可能的話,使用新的剃刀視圖引擎渲染局部的最佳方法。 我知道這是當時沒有完全完成的事情
現在我使用RenderPage來呈現用戶控件:
@RenderPage("~/Views/Shared/LocaleUserControl.cshtml",ViewData.Model)
調用RenderPage的頁面使用布局(主)頁面,其中定義了三個部分:TitleContent,HeadContent和Maincontent。 當我嘗試從此頁面渲染我的語言環境控件時,似乎還需要這些部分 - 它們應該只在調用頁面中需要並且存在。 我收到以下消息,無論我是否在部分視圖中包含這些部分(顯然我不想包含這些部分,但它似乎是一個有趣的調試點......)。
以下部分已定義,但尚未在布局頁面'〜/ Views / Shared / LocaleUserControl.cshtml'上呈現:TitleContent; HeadContent; 搜索Maincontent
我的部分觀點如下(改編自以下鏈接 ):
@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>
你看起來很像編輯器模板,所以你可以這樣包含它(假設你的部分放在~/views/controllername/EditorTemplates
子文件夾中):
@Html.EditorFor(model => model.SomePropertyOfTypeLocaleBaseModel)
或者如果不是這種情況:
@Html.Partial("nameOfPartial", Model)
如果您不想復制代碼,並且像我一樣,您只想顯示統計信息,那么您可以在視圖模型中傳遞要從中獲取數據的模型,如下所示:
public class GameViewModel
{
public virtual Ship Ship { get; set; }
public virtual GamePlayer GamePlayer { get; set; }
}
然后,在您的控制器中,只需在相應的模型上運行查詢,將它們傳遞給視圖模型並返回它,例如:
GameViewModel PlayerStats = new GameViewModel();
GamePlayer currentPlayer = (from c in db.GamePlayer [more queries]).FirstOrDefault();
[檢查結果的代碼]
//pass current player into custom view model
PlayerStats.GamePlayer = currentPlayer;
就像我說的那樣,如果你想顯示相關表格中的統計數據,你應該真的這樣做,並且出於其他人在上面提到的安全原因,沒有CRUD過程的其他部分發生。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.