简体   繁体   中英

CRUD operation, EntityFramework: Passing TextBox value(Text) to a method that takes class object as parameter

I am performing CRUD operations using EF. I have an entity defined as Person_T and this is passed as a parameter to a method that performs update operation.

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();
    }

I am retrieving the TextBox values for the fields in my CodeBehind file as follows:

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 );

        }

    }

I do not yet know how I can pass these TextBox values to the UpdateEmployeeDetails() method. The Person_T has the following definition:

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; }
}

I understand that I might not be using the best programming practices, but it would be of great help if someone could shed some light on this matter.

Thank you

有什么理由不这样做吗?

OBJECTCONT.UpdateEmployeeDetails(inte,dtobj);

Create an object of Person_T as below and pass it into UpdateEmployeeDetails():

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

and then call the method:

OBJECTCONT.UpdateEmployeeDetails(inte,_person);

Complete code:

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);    
        }    
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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