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

MVC NotMapped attribute EditorFor returning null value in Edit.cshtml but not Create.cshtml
2020-11-24
  •  译文(汉语)
  •  原文(英语)

此问题与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,其他属性具有正确的值.我什至移动了此属性,因此它不会成为最后一个属性,并且此属性正确无误.

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

解决过程1

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

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

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

[HttpPost]
    public ActionResult Edit(MyModel myModel)

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

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

This question relates to a Visual Studio 2013, Asp.net MVC-5.2.2 project:

I have a NotMapped string attribute in my class:

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

I use this attribute, which is not mapped in the original table of the Controller, to fill a column in a different table that has a foreign key relationship with the Controller's table under consideration. Again, this idea works perfectly in Create, but not edit.

I have an MVC EditorFor box that works in the Create.cshtml (razor) as follows:

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

However, the exact same razor code returns null when used in the Edit.cshtml file.

In the Edit post-back method of the controller, this attribute (and only this attribute) is null the other attributes have the correct values. I even moved this attribute so it wouldn't be the last attribute, and one after it was correct.

Any ideas as to why this would work in Create and not Edit?

Solutions1

Turns out that a new default project that I used had some bind attributes in the controller's post-back Edit method:

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

One can just omit this and it will work:

[HttpPost]
    public ActionResult Edit(MyModel myModel)

Presumably, adding this attribute to the bind list will also work, though I didn't check that personally myself.

NOTE: I really didn't know the answer to this question, but pondering the mystery, I eventually saw those attributes. I didn't even think the method worth putting in the original question as I was fairly certain that it would be the Razor code that was a problem. Also, I was originally using a dropdown that wasn't working, and decided to replace it with a textbox for simplicity; that turned out to be a wise decision, as one can imagine the number of red-herrings in debugging the textbox!

转载于:https://stackoverflow.com/questions/28158182/mvc-notmapped-attribute-editorfor-returning-null-value-in-edit-cshtml-but-not-cr

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入