簡體   English   中英

調用將模型列表作為Web表單參數的Web服務

[英]Calling a web service that has list of a model as a parameter to a web form

我的服務看起來像這樣...

    [WebMethod]
    public bool insertRecord(List<People> stu)
    {
        using (LearnershipDBEntities dn = new LearnershipDBEntities())
        {
            Person obj = new Person();

            foreach (var item in stu)
            {
                obj.personID = item.personId;
                obj.dateOfBirth = item.dob;
                obj.idNumber = item.idNumber;
                obj.name = item.name;
                obj.title = item.title;
            }

            dn.People.Add(obj);
            dn.SaveChanges();

        }

        return true;
    }

我的表格有此代碼

private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            ToDbXml.Service2 nb = new ToDbXml.Service2();
           Service2Client s = new Service2Client();

            People dto = new People();

            for (int i = 0; i <= 4; i++)
            {                   
                dto.name = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[0].Value.ToString()) ? string.Empty : dataGridView1.Rows[i].Cells[0].Value.ToString();
                dto.dob = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[1].Value.ToString()) ? (DateTime)System.Data.SqlTypes.SqlDateTime.Null : DateTime.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
                dto.title = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[2].Value.ToString()) ? string.Empty : dataGridView1.Rows[i].Cells[2].Value.ToString();
                dto.idNumber = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value.ToString()) ? string.Empty : dataGridView1.Rows[i].Cells[3].Value.ToString();                                     
            }

            s.insertRecord(dto); 


            MessageBox.Show("Values Inserted Succesfully");
        }
        catch (SqlException ex)
        {
            MessageBox.Show( ex.Message);
        }

    }

當我在傳遞給我的服務的對象上懸停時,出現此錯誤。 不能在人與人之間轉換[]

您需要構造一個人員列表並將其傳遞給webservice方法。 因此,我建議您像這樣修改Web服務的源代碼:

[WebMethod]
public bool insertRecord(List<People> stu)
{
    using (LearnershipDBEntities dn = new LearnershipDBEntities())
    {
        Person obj = new Person();

        foreach (var item in stu)
        {
            obj.personID = item.personId;
            obj.dateOfBirth = item.dob;
            obj.idNumber = item.idNumber;
            obj.name = item.name;
            obj.title = item.title;
            dn.People.Add(obj);
        }

        dn.SaveChanges();

    }

    return true;
}

和事件按鈕處理程序是這樣的:

private void button2_Click(object sender, EventArgs e)
{
    try
    {
        ToDbXml.Service2 nb = new ToDbXml.Service2();
        Service2Client s = new Service2Client();
        List<People> listOfPeople = new List<People>();

        for (int i = 0; i <= 4; i++)
        {    
            listOfPeople.add (
            new People() 
            {

            name = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[0].Value.ToString()) ? string.Empty : dataGridView1.Rows[i].Cells[0].Value.ToString(),
            dob = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[1].Value.ToString()) ? (DateTime)System.Data.SqlTypes.SqlDateTime.Null : DateTime.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString()),
            title = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[2].Value.ToString()) ? string.Empty : dataGridView1.Rows[i].Cells[2].Value.ToString(),
            idNumber = string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[3].Value.ToString()) ? string.Empty : dataGridView1.Rows[i].Cells[3].Value.ToString()
          });

        }
        s.insertRecord(listOfPeople); 
        MessageBox.Show("Values Inserted Succesfully");
    }
    catch (SqlException ex)
    {
        MessageBox.Show( ex.Message);
    }
}

希望這能解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM