-1

我对 IEnumerable 的 foreach 循环有问题

函数 Get_Contacts_By_Company_ID

public List<Contact> Get_Contacts_By_Company_ID(string company_id)
    {
        List<Contact> contacts = new List<Contact>();


        foreach (DataRow contacts_row in contacts_da.Get_Contacts_By_Company_ID(company_id).Rows)
        {
            Contact contact = new Contact();

            contact.Contact_ID = contacts_row["Contact_ID"].ToString();
            contact.Contact_Is_Active = Convert.ToInt32(contacts_row["Is_Active"].ToString());
            contact.Firstname = contacts_row["Firstname"].ToString();
            contact.Middlename = contacts_row["Middlename"].ToString();
            contact.Lastname = contacts_row["Lastname"].ToString();
            contact.Contact_Address = contacts_row["Address"].ToString();
            contact.Contact_City_Province = contacts_row["City_Province"].ToString();
            contact.Contact_State = contacts_row["State"].ToString();
            contact.Contact_ZIP = contacts_row["ZIP"].ToString();
            contact.Contact_Phone = contacts_row["Phone"].ToString();
            contact.Contact_Mobile_Phone = contacts_row["Mobile_Phone"].ToString();

            contacts.Add(contact);

        }


        return contacts;
    }

contacts_da.Get_Contacts_By_Company_ID 函数只会返回一个 DataTable

控制器:

 [Authorize]
    public ActionResult Edit(string id)
    {

        List<Contact> contacts = new List<Contact>();

        Contact contact = new Contact();

        contacts = contact.Get_Contacts_By_Company_ID(id);

        return View(contacts);
    }

    [HttpPost]
    public ActionResult Edit(List<Contact> contact)
    {

        List<Contact> contacts = contact;

        //company.Update_Company(company_model);

        return View();
    }

看法:

@model IEnumerable<DTIMS_BL.Contact>
@using (Html.BeginForm("Edit/" + Model, "Company", FormMethod.Post))
{
@foreach (var item in Model)
{
<table>
  <tr>
     <td>
       @Html.DisplayFor(m => item.Firstname)                      

        </td>
        <td>
          @Html.ActionLink("Remove", "", new { @class = "btn small fr" })
         </td>
          <td>
           @Html.ActionLink("Edit", "", new { @class = "btn small fr" })
          </td>
      </tr>
</table>
  } 
<button class="fr submit">Save Company Details</button>
}

当我单击将提交表单的“保存公司详细信息”按钮时,问题就开始了。我收到“NullReferenceException”。我在这里错过了什么吗?请帮忙。

4

3 回答 3

1
@using (Html.BeginForm("Edit", "Company", FormMethod.Post))

代替

@using (Html.BeginForm("Edit/" + Model, "Company", FormMethod.Post))
于 2012-10-10T14:00:16.950 回答
0

您可以添加此代码check DataTable value,如果表为空,则返回空列表

List<Contact> Get_Contacts_By_Company_ID(string company_id)
{
      List<Contact> contacts = new List<Contact>();
      var table = contacts_da.Get_Contacts_By_Company_ID(company_id);
      if(table == null)
      {
         return contacts;
      }

      foreach (DataRow contacts_row in contacts_da.Get_Contacts_By_Company_ID(company_id).Rows)
      {
       ....
      }
}
于 2012-10-10T13:58:28.940 回答
0

我假设其中一行中的一列是null. 您应该避免ToString()在这种情况下使用,Convert.ToString()而是使用:

contact.Contact_ID = Convert.ToString(contacts_row["Contact_ID"]);
contact.Contact_Is_Active = Convert.ToInt32(contacts_row["Is_Active"]);
contact.Firstname = Convert.ToString(contacts_row["Firstname"]);
contact.Middlename = Convert.ToString(contacts_row["Middlename"]);
contact.Lastname = Convert.ToString(contacts_row["Lastname"]);
contact.Contact_Address = Convert.ToString(contacts_row["Address"]);
contact.Contact_City_Province = Convert.ToString(contacts_row["City_Province"]);
contact.Contact_State = Convert.ToString(contacts_row["State"]);
contact.Contact_ZIP = Convert.ToString(contacts_row["ZIP"]);
contact.Contact_Phone = Convert.ToString(contacts_row["Phone"]);
contact.Contact_Mobile_Phone = Convert.ToString(contacts_row["Mobile_Phone"]);
于 2012-10-10T13:59:10.030 回答