2016-06-13 57 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; } 
} 
}