[英]Working with EF Generated Model and My Own Model
我有2個模型。 第一個由EF創建,如下所示:
public partial class PrinterMapping
{
public string MTPrinterID { get; set; }
public string NTPrinterID { get; set; }
public string Active { get; set; }
}
我創建了第二個數據庫(與任何數據庫表無關),如下所示:
public class ExceptionModel
{
public string ExceptionMessage { get; set; }
public ExceptionModel(string exceptionMessage)
{
ExceptionMessage = exceptionMessage;
}
}
在“索引”和“創建”視圖中,自動傳遞的模型是PrinterMapping。 在保存到PrinterMapping訪問的表后,我希望以相關方式填充ExceptionModel模型的ExceptionMessage屬性。 因此,在我的Create控制器中,我正在執行以下操作:
ExceptionModel exModel = new ExceptionModel(message);
其中message參數是一個相關的字符串,例如“打印機X已經存在”。
我的想法是要有一個名為ExceptionMessageView的局部視圖,將ExceptionModel傳遞給它,然后我將顯示:
@Html.DisplayFor(model => model.ExceptionMessage)
在“索引”和“創建”視圖中,我將顯示以下行:
@Html.Partial("~/Views/Home/ExceptionMessageView.cshtml")
我是否使事情復雜化? 無論如何這是行不通的,因為我不完全了解如何將填充的ExceptionModel從我的Create Controller傳遞到ExceptionMessageView局部視圖。
請問一個善良的靈魂開悟嗎?
我將擁有一個具有ExceptionModel和PrinterMapping屬性的復雜ViewModel“ PrinterViewModel”。
然后,控制器將完整的PrinterViewModel傳遞給視圖。
在視圖中,您可以通過將部分復雜的Viewmodel傳遞給局部來渲染局部。
@Html.Partial("ExceptionMessageView",Model.Exception)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.