如何将Class值赋给另一个类MVC

时间:2015-12-15 18:05:36

标签: c#

我正在尝试使用MVC架构制作程序,但我的模型类值不会发送到控制器。如果我在模型类中编写代码以向数据库发送值,那么它可以工作,但是当我在控制器类中编写代码以将值发送到数据库时,它不起作用。

这是模型类:

namespace MVCC.Control
{
    class DogControl
    {       
         private string Name;
         private int Age;

         public DogControl()
         {
         }
         public DogControl(string name, int age)
         {
             Name = name;
             Age = age;
         }

        public void SetName(string name)
        {
            Name = name;
        }
        public string GetName()
        {
            return Name;
        }
        public void SetAge(int age)
        {
            Age = age;
        }
        public int GetAge()
        {
            return Age;
        }

这是控制器类:

class Dog : DogControl
    {
       //DogControl();
        public string Name { get; set; }
        public int Age { get; set; }
        DogControl ctrl = new DogControl();

 public void update()
        {


            try
            {
                Connection newcon = new Connection();
                newcon.connnew();
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = Connection.conect;

                cmd.CommandText = "UPDATE neelas.emp_login SET name='" +GetName() + "' WHERE emp_id = '" + GetAge() + "' ";
                cmd.Parameters.AddWithValue("@emp_id", GetAge());
                cmd.Parameters.AddWithValue("@name",GetName());
                cmd.ExecuteNonQuery();

                MessageBox.Show("Data update in database");
                MessageBox.Show(GetName());
               // MessageBox.Show(conGetAge());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

这是我的按钮点击事件:

   private void update_Click(object sender, EventArgs e)
    {
        string Name = nameTxt.Text;
        int Age = Convert.ToInt32(AgeTxt.Text);

        Dog dg = new Dog();
        dg.update();


    }

1 个答案:

答案 0 :(得分:0)

public class Dog
{
    private string _name;
    private int _age;

    public Dog(string name, int age)
    {
        this._name = name;
        this._age = age;
    }

    public Dog setName(string name)
    {
        this._name = name;

        return this;
    }

    public string getName()
    {
        return _name;
    }

    public Dog setAge(int age)
    {
        this._age = age;

        return this;
    }

    public int getAge()
    {
        return this._age;
    }   
}

public class DogControl
{
    private Dog _dog;

    public DogControl(Dog dog)
    {
        _dog = dog;
    }

    public void update()
    {
        try
        {
            Connection newcon = new Connection();
            newcon.connnew();
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = Connection.conect;
            cmd.CommandText = "UPDATE neelas.emp_login SET name=@name WHERE emp_id = @emp_id ";
            cmd.Parameters.AddWithValue("@emp_id", _dog.getAge());
            cmd.Parameters.AddWithValue("@name",_dog.getName());
            cmd.ExecuteNonQuery();

            MessageBox.Show("Data update in database");
            MessageBox.Show(GetName());
            // MessageBox.Show(conGetAge());
        }
        catch (Exception ex)
        {
           MessageBox.Show(ex.Message);
        }
    }
}

private void update_Click(object sender, EventArgs e)
{
    string Name = nameTxt.Text;
    int Age = Convert.ToInt32(AgeTxt.Text);

    Dog dg = new Dog(Name, Age);
    DogControl control = new DogControl(dg);

    dg.update();
}