繁体   English   中英

MVC NotMapped属性Editor用于在Edit.cshtml中返回空值,但不能在Create.cshtml中返回

[英]MVC NotMapped attribute EditorFor returning null value in Edit.cshtml but not Create.cshtml

此问题与Visual Studio 2013,Asp.net MVC-5.2.2项目有关:

我的课上有一个NotMapped字符串属性:

    [NotMapped]
    public string myNotMappedAttribute{ get; set; }

我使用此属性(未映射到Controller的原始表中)来填充不同表中的列,该表与正在考虑的Controller表具有外键关系。 同样,此想法可在“创建”中完美运行,但不可编辑。

我有一个MVC EditorFor框,可在Create.cshtml(剃刀)中使用,如下所示:

    <div class="editor-label">
        @Html.LabelFor(model => model.myNotMappedAttribute)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.myNotMappedAttribute)
        @Html.ValidationMessageFor(model => model.myNotMappedAttribute)
    </div>

但是,在Edit.cshtml文件中使用时,完全相同的剃刀代码将返回null。

在控制器的Edit post-back方法中,此属性(且仅此属性)为null,其他属性具有正确的值。 我什至移动了此属性,因此它不会是最后一个属性,并且它是正确的。

关于为什么在“创建”而不是“编辑”中起作用的任何想法?

原来,我使用的一个新的默认项目在控制器的回发后Edit方法中具有一些绑定属性:

public ActionResult Edit([Bind(Include = "myAttribute_1,myAttribute_2,myAttribute_3")] MyModel myModel)

可以忽略这一点,它将起作用:

[HttpPost]
    public ActionResult Edit(MyModel myModel)

大概将这个属性添加到绑定列表中也可以工作,尽管我自己并没有亲自检查。

注意:我真的不知道这个问题的答案,但是仔细考虑这个谜团,我最终看到了那些属性。 我什至不认为值得在原始问题中提出该方法的方法,因为我相当确定这将是Razor代码成为问题。 另外,我最初使用的是无法使用的下拉菜单,为了简单起见,决定将其替换为文本框。 事实证明,这是一个明智的决定,因为可以想象调试文本框时会遇到很多红鲱鱼!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM