2016-06-13 39 views
0

我會用在很多不同的頁面,這將是不同的僅僅是參考模型(@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) 
+1

爲什麼你使用'dynamic'反正?所有模型都有一個你想訪問的特定屬性嗎?然後讓他們實現一個接口,然後使用強類型化到該接口的模型。 – mason

+0

動態是一個非常糟糕的方式來使用asp.net-mvc。 –

回答

0

檢查從您的操作方法或Razor視圖空

動態關鍵字在C#4.0允許你做一個動態模型綁定

dynamic viewModel = new ExpandoObject(); 
    viewModel.TestString = "This is a test string"; 

return View(viewModel); for more in refer the below link 

examles

Dynamic model binding with examle

0

基於這條消息,我認爲你不能使用動態操作的「for」後綴的方法。

從來沒有使用過,但你可以嘗試generic form

@Html.Label("Name", "Name Label", htmlAttributes: new { @class = "control-label col-md-2" }) 

的語法說的表達,所以我相信它會支持點符號。

0

我想出了一個簡單的方法,如果你確定了基本的CSS和沒有什麼花哨。

您可以使用「EditForModel()」在你的模型顯示的所有屬性,而不是「EditFor」每個屬性。在此之後,對於您不希望在視圖中顯示的每個屬性,可以在模型中添加「[ScaffoldColumn(false)]」,如下所示。感謝所有的建議!!!

部分

@model dynamic 

@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 
<div class="container creative-containter-top"> 
    <div class="row"> 
     <div class="col-sm-offset-4 col-sm-4"> 
      <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" }) 

         <div class="col-sm-offset-2"> 
          <div class="form-group"> 
           @Html.EditorForModel() 
          </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> 
} 

型號

public partial class CreativeOps 
{ 
    public CreativeOps() 
    { 
     CreativeNames = new HashSet<CreativeNames>(); 
    } 
    [ScaffoldColumn(false)] 
    public int Id { get; set; } 
    [Required] 
    public string Code { get; set; } 
    [Required] 
    public string Name { get; set; } 
    [Required] 
    public bool Disabled { get; set; } 

    public virtual ICollection<CreativeNames> CreativeNames { get; set; } 
} 
}