CRUD操作,EntityFramework:将TextBox值(Text)传递给以类对象作为参数的方法

时间:2015-04-19 06:28:52

标签: c# asp.net entity-framework

我正在使用EF执行CRUD操作。我有一个定义为Person_T的实体,它作为参数传递给执行更新操作的方法。

public void UpdateEmployeeDetails(int personID, Person_T employee)
    {
        Person_T emp = new Person_T();

        emp = Context1.Person_T.Where(x => x.Person_IDNO == personID).FirstOrDefault();

        emp.FirstName = employee.FirstName;
        emp.LastName = employee.LastName;
        emp.CreatedDate = DateTime.Now;
        emp.UpdatedDate = DateTime.Now;

        Context1.SaveChanges();
    }

我正在检索CodeBehind文件中字段的TextBox值,如下所示:

protected void UpdateDetails_Click(object sender, EventArgs e)
    {
        if(IsPostBack)
        {
            dtobj.firstNmae = TextBoxFN.Text;
            dtobj.lastName = TextBoxLN.Text;
            QS = Request.QueryString["ReqID"];
            inte = Convert.ToInt32(QS);
            OBJECTCONT.UpdateEmployeeDetails(inte,TextBOXValues );

        }

    }

我还不知道如何将这些TextBox值传递给UpdateEmployeeDetails()方法。 Person_T具有以下定义:

public partial class Person_T
{
    public int Person_IDNO { get; set; }
    public Nullable<int> Address_IDNO { get; set; }
    public string FirstName { get; set; }
    public Nullable<System.DateTime> CreatedDate { get; set; }
    public string LastName { get; set; }
    public Nullable<System.DateTime> UpdatedDate { get; set; }

    public virtual Address_Table Address_Table { get; set; }
}

我知道我可能没有使用最好的编程实践,但如果有人能够对这个问题有所了解,那将会有很大的帮助。

谢谢

2 个答案:

答案 0 :(得分:0)

有什么理由不这样做?

OBJECTCONT.UpdateEmployeeDetails(inte,dtobj);

答案 1 :(得分:0)

如下所示创建Person_T的对象并将其传递给UpdateEmployeeDetails():

Person_T _person = new Person_T(){
     FirstName = TextBoxFN.Text,
     LastName = TextBoxLN.Text,
     //assign other values as above
};

然后调用方法:

OBJECTCONT.UpdateEmployeeDetails(inte,_person);

完整代码:

protected void UpdateDetails_Click(object sender, EventArgs e)
    {
        if(IsPostBack)
        {
             Person_T _person = new Person_T(){
                  FirstName = TextBoxFN.Text,
                  LastName = TextBoxLN.Text                      
             };

            QS = Request.QueryString["ReqID"];
            inte = Convert.ToInt32(QS);
            OBJECTCONT.UpdateEmployeeDetails(inte, _person);    
        }    
    }