我會用在很多不同的頁面,這將是不同的僅僅是參考模型(@model ...)相同的部分錯誤。我試着用「@model動態」這樣我就可以在觀看基準模型,但是lambda表達式是在我的部分被引用特定屬性都扔了以下錯誤:使用使用剃刀語法諧音動態模型拋出
"An expression tree may not contain a dynamic operation"
caused by the following reference to model:
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
如何構建我的代碼或創建一個模型引用將動態工作,以便我可以在我的視圖頁面中引用每個特定的模型實例?我在網上嘗試了很多解決方案,到目前爲止沒有任何工作。以下是我的部分觀點。任何幫助將不勝感激!!!
部分:
@model dynamic
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="container creative-containter-top">
<div class="row">
<div class="col-sm-offset-2 col-sm-8">
<div class="form-horizontal">
<div class="panel panel-primary">
<div class="panel-height pane panel-heading text-center">
<h3>@ViewBag.Title</h3>
</div>
<div class="panel-body">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Code, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Code, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Code, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Code, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Code, new { htmlAttributes = new { @class = "checkbox center-block" } })
@Html.ValidationMessageFor(model => model.Code, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</div>
</div>
<div class="panel-footer">
@Html.ActionLink("Back to List", "Index")
</div>
</div>
</div>
</div>
</div>
</div>
}
查看
@model CreativeNamingConvention.Models.Domain.CreativeOps
@{
ViewBag.Model = Model;
ViewBag.Title = "Edit Creative Ops Field";
}
@Html.Partial("~/Views/Templates/editBody.cshtml", Model)
爲什麼你使用'dynamic'反正?所有模型都有一個你想訪問的特定屬性嗎?然後讓他們實現一個接口,然後使用強類型化到該接口的模型。 – mason
動態是一個非常糟糕的方式來使用asp.net-mvc。 –