如何在MVC中绑定对象? [英] How to bind objects in MVC?

查看:101
本文介绍了如何在MVC中绑定对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家下午好,



我的任务是通过在线教程学习MVC并在新的Web应用程序上实现这些知识。所以我对MVC来说有点绿。



到目前为止我还没有解决的一个问题是如何在使用Entity Framework时绑定对象。检索具有基本数据类型的类没有问题,但现在我创建了一个Survey类,其中包含Outcome类的属性。 'Outcome'是一个引用表,包含ID和Description列,并且正在成功填充。在数据库中,EF创建了一个名为'Outcome_ID'的外键。但是,当数据访问层(继承DbContext)编译DBSet(Survey)时,尽管确保所有数据库中都有有效的外键,但结果字段仍保持为空。



调查类:

Good afternoon all,

I have been tasked with learning MVC through online tutorials and implementing this knowledge on a new web application. So I am a little green when it comes to MVC.

One issue I haven't solved so far is how to bind objects when using the Entity Framework. Classes with basic data types are retrieved with no issues, but now I created a 'Survey' class which includes a property of class 'Outcome'. 'Outcome' is a reference table, containing ID and Description columns, and is being populated successfully. In the database, the EF created a foreign key named 'Outcome_ID'. But when the Data Access Layer (which inherits the DbContext) compiles the DBSet(of Survey), the Outcome field is remaining null, despite making sure that all have valid Foreign Keys in the database.

Survey Class:

Imports System.ComponentModel.DataAnnotations
Public Class Survey

    <Key>
    <Schema.DatabaseGenerated(Schema.DatabaseGeneratedOption.Identity)> Public Property ID As Integer
    Public Property Created As DateTime
    Public Property LastModified As DateTime
    Public Property Owner As String
    Public Property Outcome As Outcome
    Public Property SurveyCountry As Integer

End Class





数据访问层:



Data Access Layer:

Imports System.Data.Entity

Public Class DAL
    Inherits DbContext

    Public Property Users As DbSet(Of User)
    Public Property Surveys As DbSet(Of Survey)
    Public Property Outcomes As DbSet(Of Outcome)

    Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
        modelBuilder.Entity(Of User).ToTable("User")
        modelBuilder.Entity(Of Survey).ToTable("Survey")
        modelBuilder.Entity(Of Outcome).ToTable("Outcomes")
        MyBase.OnModelCreating(modelBuilder)
    End Sub
End Class





Repository Class Functi on(这将返回一个调查列表,每个调查都有一个null'结果'):



Repository Class Function (this returns a list of surveys, each with a null 'Outcome'):

Public Function GetSurveyList() As List(Of Survey)
    If IsNothing(data) Then data = New DAL()
    Return data.Surveys.ToList()
End Function





我确信我需要在字段上添加某种形式的绑定,或初始化或其他东西。我错过了什么?在此先感谢您的帮助!



我尝试过:



在线查看,但无法找到解决方案。



I am convinced that I need to add some form of binding on the field, or an initialisation, or something. What am I missing? Thanks in advance for your help!

What I have tried:

Looked this up online, but could not find a solution.

推荐答案

感谢您的回复!我设法以两种不同的方式使它工作:



我尝试过理查德的解决方案,但没有奏效。但稍微调整一下:

Thanks for your replies all! I managed to make it work in two different ways:

I tried Richard's solution, but did not work. But with a little tweaking it did:
Return data.Surveys.Include("Outcome").ToList()



然后我读了这篇文章:懒惰,急切和显式加载相关数据并意识到如果我使该属性'Overridable ', 有效。这是VB.NET的C#'virtual'关键字。如果我理解正确的话,这个解决方案应该比第一个解决方案在数据库上产生更少的开销。


Then I read this article: Lazy, Eager, and Explicit Loading of Related Data and realised that if I made the property 'Overridable', it worked. This is VB.NET's keyword for C#'s 'virtual'. This solution should create a little less overhead on the database than the first solution, if I understood correctly.

Public Overridable Property Outcome As Outcome



谢谢,



Charles。


Thanks,

Charles.


这篇关于如何在MVC中绑定对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆