C# 如何将值从实体层传递到表示层
我试图在点击gridview中的linkbutton时填充Person对象,然后获取其相关数据并显示在模式中的文本框中。我能够获取数据并将其传递给Person对象,但是,当我填充模态控件时,Person对象在所有属性上返回null 您知道如何在不调用Person objPerson=new Person()的情况下将Person对象的值传递给textbox控件吗 先谢谢你 实体:C# 如何将值从实体层传递到表示层,c#,asp.net,oop,C#,Asp.net,Oop,我试图在点击gridview中的linkbutton时填充Person对象,然后获取其相关数据并显示在模式中的文本框中。我能够获取数据并将其传递给Person对象,但是,当我填充模态控件时,Person对象在所有属性上返回null 您知道如何在不调用Person objPerson=new Person()的情况下将Person对象的值传递给textbox控件吗 先谢谢你 实体: public class Person { public int PersonID { get; set;
public class Person
{
public int PersonID { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
数据层:
public DataTable SelectQueryProc(String _storedProc, SqlParameter[] sqlParameter)
{
DataTable t = new DataTable();
try
{
using (SqlConnection sqlConnection = new SqlConnection(CommonEntity.ConnectionString))
{
sqlConnection.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter(_storedProc, sqlConnection))
{
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.SelectCommand.Parameters.AddRange(sqlParameter);
adapter.Fill(t);
}
}
}
catch (SqlException e)
{
throw new SystemException(e.ToString());
}
finally
{
}
return t;
}
public List<Person>GetPersonSingleByPersonID(string personID)
{
List<Person> objPerson = new List<Person>();
DataTable dt = new DataTable();
SqlParameter[] sqlParam = new SqlParameter[1];
sqlParam[0] = new SqlParameter("@PersonID", SqlDbType.VarChar);
sqlParam[0].Value = personID;
CommonDAL commonDAL = new CommonDAL();
dt = commonDAL.SelectQueryProc("GetPersonSingleByPersonID", sqlParam);
foreach (DataRow dr in dt.Rows)
{
objPerson.Add(new Person()
{
PersonID = dr["PersonID"].ToString(),
Firstname = dr["Firstname"].ToString(),
Lastname = dr["Lastname"].ToString()
});
}
return objPerson;
}
您尝试从空的Person对象获取值,而不是从DAL获取值。所以你的方法应该是这样的
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "click":
{
// Populate Person Details
PersonBL objPersonBL = new PersonBL();
var objPerson = objPersonBL.GetPersonSingleByPersonID(e.CommandArgument.ToString());
//Person objPerson = new Person();
if (objPerson.Count != 0)
{
txtPersonID.Text = objPerson[0].PersonID;
txtFirstname.Text = objPerson[0].Firstname;
txtLastname.Text = objPerson[0].Lastname;
break;
}
}
default:
break;
}
}
很明显,您正在将新创建的对象(尚未填充)值分配给文本框。 您的代码应该是:
PersonBL objPersonBL = new PersonBL();
Person objPerson = objPersonBL.GetPersonSingleByPersonID(e.CommandArgument.ToString());
if (objPerson.Count != 0)
{
txtPersonID.Text = objPerson.PersonID;
txtFirstname.Text = objPerson.Firstname;
txtLastname.Text = objPerson.Lastname;
}
您是否调试了此代码,并在DAL中填充人员时进行了演示?是的,已填充。我认为演示文稿中的new Person()会导致文本框出现空值Person构造函数是否将Firstname、PersonID和Lastname设置为
string.Empty
,或者将它们保留为默认值null
?public int PersonID{get;set;}我认为默认值为空
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "click":
{
// Populate Person Details
PersonBL objPersonBL = new PersonBL();
var objPerson = objPersonBL.GetPersonSingleByPersonID(e.CommandArgument.ToString());
//Person objPerson = new Person();
if (objPerson.Count != 0)
{
txtPersonID.Text = objPerson[0].PersonID;
txtFirstname.Text = objPerson[0].Firstname;
txtLastname.Text = objPerson[0].Lastname;
break;
}
}
default:
break;
}
}
PersonBL objPersonBL = new PersonBL();
Person objPerson = objPersonBL.GetPersonSingleByPersonID(e.CommandArgument.ToString());
if (objPerson.Count != 0)
{
txtPersonID.Text = objPerson.PersonID;
txtFirstname.Text = objPerson.Firstname;
txtLastname.Text = objPerson.Lastname;
}