使用Razor语法在partials中使用动态模型会引发错误

时间:2016-06-13 01:54:27

标签: c# asp.net razor asp.net-web-api lambda

我将在许多不同的页面中使用相同的部分,唯一不同的是对模型的引用(@model ...)。我尝试使用“@model dynamic”,所以我可以在视图中引用模型,但是我的partial中引用特定属性的lambdas会引发以下错误:

  

“表达式树可能不包含动态操作”

     

由以下对模型的引用引起:

     

@ 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)

3 个答案:

答案 0 :(得分:0)

从您的操作方法或剃刀视图中检查null

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

答案 1 :(得分:0)

根据该消息,我假设你不能在动态操作中使用“For”后缀方法。

从未使用过它们,但您可以尝试generic form

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

语法表达式,所以我认为它会支持点表示法。

答案 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; }
}
}