简体   繁体   English

如何在C#中将值从Entity传递到Presentation

[英]How can I pass values from Entity to Presentation in c#

How can I pass the value from Entity Layer to Presentation Layer. 如何将值从实体层传递到表示层。 I have populated the object Person but when I call it in Presentation it became null. 我已经填充了对象Person,但是当我在Presentation中调用它时,它变为空。

Can anyone help me. 谁能帮我。 Thanks in advance! 提前致谢!

Entity: 实体:

public class Person
{
    public int PersonID { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}

DataLayer: 数据层:

public List<Person> GetPersonSingleByPersonID(string personID)
{
    List<Person> objPerson = new List<Person>();
    DataTable dt = new DataTable();

    ...

    foreach (DataRow dr in dt.Rows)
    {
        objPerson.Add(new Person()
        {
            PersonID = dr["PersonID"].ToString(),
            Firstname = dr["Firstname"].ToString(),
            Lastname = dr["Lastname"].ToString()            
        });
    }
    return objPerson;
}

PresentationLayer: PresentationLayer:

Person objPerson = new Person(); //I think error goes here
txtPersonID.Text = objPerson.PersonID;
txtFirstname.Text = objPerson.Firstname;
txtLastname.Text = objPerson.Lastname;

There are a few issues with your code: 您的代码存在一些问题:

  1. Why GetPersonSingleByPersonID(string personID) returns a list? 为什么GetPersonSingleByPersonID(string personID)返回一个列表? If you want to return a single person your method should be defined as Person GetPersonSingleByPersonID(string personID) 如果你想返回一个人你的方法应该被定义为Person GetPersonSingleByPersonID(string personID)

  2. You are not calling GetPersonSingleByPersonID(string personID) method at all. 您根本没有调用GetPersonSingleByPersonID(string personID)方法。 Instead you are creating a new 'blank' instance of Person class ( Person objPerson = new Person() ) You need to call GetPersonSingleByPersonID(id) and since you're returning a list instead of a single object (see point 1.) you probably want to add .FirstOrDefault() . 相反,您正在创建Person类的新“空白”实例( Person objPerson = new Person() ),您需要调用GetPersonSingleByPersonID(id)并且由于要返回列表而不是单个对象(请参见第1点)。可能想添加.FirstOrDefault()

Person objPerson = GetPersonSingleByPersonID(id).FirstOrDefault()

you need to call the GetPersonaSingleByPersonId() method in your data access layer. 您需要在数据访问层中调用GetPersonaSingleByPersonId()方法。 create an instance of your DAL and then call the method. 创建DAL的实例,然后调用该方法。 this will return the object you want with the details and then you finally assign it to the textboxes. 这将返回您想要的对象以及详细信息,然后最终将其分配给文本框。

remember though, the method returns a collection... so you would be overwriting the previous values in your textbox if you are going through the entire collection and writing it to the textbox. 不过请记住,该方法返回一个集合...因此,如果您要遍历整个集合并将其写入文本框,则将覆盖文本框中的先前值。

最好在数据表示之间有一个业务层,在该业务层中,您可以从数据层获取实体并进行处理,然后转换为表示层对象。

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

相关问题 如何将值从实体层传递到表示层 - How can I pass values from Entity Layer to Presentation Layer 如何将C#表单中的值传递给Python - How can i pass values from a C# form into Python 我如何将我的imageButton值传递给C#函数 - How can i pass my imagebutton values to c# function 如何在C#中同时将datagridview的多个单元格值从一种形式传递到另一种形式? - How can I pass multiple cell values of datagridview from one form to another at same time in C#? 我怎样才能将数组从C#传递到一个非托管DLL中,并以long []形式获取值? - How can I pass an array from C# to an unmanaged DLL as a long [] and get values back? 如何将指针从C#传递给非托管DLL? - How can I pass a pointer from C# to an unmanaged DLL? 如何将TRUE从JavaScript传递到C#脚本 - How can I pass TRUE from javascript to a C# script 在C#/ html上使用实体框架时,如何将多个参数传递给Actionlink() - How can I pass multiple parameters to Actionlink() while working with entity framework on c#/html 如何将实体对象传递给C#中的函数 - How do I pass an Entity Object to a function in C# 如何将表单值(例如电子邮件,姓名,电话等)从C#控制台应用程序传递到任何网站? - How can I pass form values like email, name, phone etc from my C# console application to any website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM