[英]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.