我对 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”。我在这里错过了什么吗?请帮忙。