0
我使用EF6與我的項目。我不得不將包含兩個獨立類的類返回到我的視圖。對不起,使用非通用名稱和使用行話類名表單結果&實體框架有不同的輸入名稱
的模型回到我如下:
public class Configuration
{
public List<ProductEntity> ProductCategories { get; set; }
public PricingConfigurationLog PricingConfiguration { get; set; }
}
下面是使用剃刀語法窗體字段輸入的一個例子。
剃刀語法
@Html.EditorFor(model => model.PricingConfiguration.PromoText, new { htmlAttributes = new { @class = "form-control" } })
HTML輸出
<input class="form-control text-box single-line" id="PricingConfiguration_PromoText" name="PricingConfiguration.PromoText" type="text" value="Promotional Text">
當通過表單元素放回我的控制器來處理它們爲的EntityFramework更新/插入到數據庫中的列是空的。
這似乎是顯而易見的元素形成名字被呈現爲name="PricingConfiguration.PromoText
,而不是PromoText
這是我不知道如何改變渲染或低於約束力。
public ActionResult SaveConfiguration([Bind(Include = "ProductCode,"
+ "PromoText,"
+ "CartLimitPerDay,"
+ "CartDayAllowed,"
+ "SessionDeleteTimeMinutes,"
+ "SessionTimeoutMinutes")] PricingConfigurationLog PricingConfigurationLog)
它看起來像你想出一個方法來「皮膚此貓」。但是,作爲建議,我會建議使用視圖模型而不是實體。更重要的是,我會建議你儘可能地嘗試並壓平你的視圖模型。這並不總是可行的,但它消除了您遇到的頭痛類型。 – trevorc