简体   繁体   English

使用EF生成的模型和我自己的模型

[英]Working with EF Generated Model and My Own Model

I have 2 Models. 我有2个模型。 First one is created by EF and looks like: 第一个由EF创建,如下所示:

public partial class PrinterMapping
{
    public string MTPrinterID { get; set; }
    public string NTPrinterID { get; set; }
    public string Active { get; set; }
}

I created the second one (nothing to do with any database table) and looks like: 我创建了第二个数据库(与任何数据库表无关),如下所示:

public class ExceptionModel
{
    public string ExceptionMessage { get; set; }

    public ExceptionModel(string exceptionMessage)
    {
        ExceptionMessage = exceptionMessage;
    }
}

In my Index and Create views, the model that is automatically being passed is PrinterMapping. 在“索引”和“创建”视图中,自动传递的模型是PrinterMapping。 I wish to output ExceptionMessage property of the ExceptionModel model after populating it in a relevant way after saving to the table accessed by PrinterMapping. 在保存到PrinterMapping访问的表后,我希望以相关方式填充ExceptionModel模型的ExceptionMessage属性。 So in my Create controller, I am doing: 因此,在我的Create控制器中,我正在执行以下操作:

ExceptionModel exModel = new ExceptionModel(message);

where message parameter is a relevant string like "Printer X already exists". 其中message参数是一个相关的字符串,例如“打印机X已经存在”。

My thoughts were to have a partial view called ExceptionMessageView where my ExceptionModel would be passed on to it and I will display: 我的想法是要有一个名为ExceptionMessageView的局部视图,将ExceptionModel传递给它,然后我将显示:

@Html.DisplayFor(model => model.ExceptionMessage)

And in my Index and Create views, I will have a line like: 在“索引”和“创建”视图中,我将显示以下行:

@Html.Partial("~/Views/Home/ExceptionMessageView.cshtml")

Am I over complicating things? 我是否使事情复杂化? This isn't working anyway since I don't fully understand how to pass on the populated ExceptionModel from my Create Controller to the ExceptionMessageView partial view. 无论如何这是行不通的,因为我不完全了解如何将填充的ExceptionModel从我的Create Controller传递到ExceptionMessageView局部视图。

Will a kind soul please enlighten? 请问一个善良的灵魂开悟吗?

I would have a complex Viewmodel "PrinterViewModel" that has properties for ExceptionModel and PrinterMapping. 我将拥有一个具有ExceptionModel和PrinterMapping属性的复杂ViewModel“ PrinterViewModel”。

The controller then passes the complete PrinterViewModel to the view. 然后,控制器将完整的PrinterViewModel传递给视图。

In the View you would render partials by passing part of the complex Viewmodel to them. 在视图中,您可以通过将部分复杂的Viewmodel传递给局部来渲染局部。

@Html.Partial("ExceptionMessageView",Model.Exception)

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

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