我不确定为什么我会收到此错误:
对象引用未设置为对象的实例。
我有一个包含一些组织的数据库,每个组织可以有多个域。我设置它的方法是只有一个常规的actionLink来删除一个域。这是代码的样子:
public ActionResult DeleteDomain(OrganizationDomainAddressView dd)
{
var org = OrganizationRepo.GetByID(dd.OrganizationID);
if (ModelState.IsValid)
{
org.DomainAddresses.Remove(new OrganizationDomainAddress(dd.OrganizationID, dd.DomainAddress));
OrganizationRepo.Delete(org);
}
OrganizationView orgInfo = (OrganizationView)Mapper.DynamicMap(org, typeof(Organization), typeof(OrganizationView));
var domainList = orgInfo.DomainAddresses;
var ACD = new OrganizationDomainAddressView();
ViewData["ACD"] = ACD;
ViewBag.OrganizationID = dd.OrganizationID;
return PartialView("_EmailDomainPartial", domainList);
}
以下是我正在使用的视图:
@model IEnumerable<AdminTool.Models.ViewModel.OrganizationDomainAddressView>
@using AdminTool.Models.ViewModel;
@{
OrganizationDomainAddressView ACD = (OrganizationDomainAddressView) ViewData["ACD"];
}
<div id="domains">
@foreach (var item in Model)
{
<div class="item">
@item.DomainAddress — @Html.ActionLink("Delete", "DeleteDomain", "Organizations", new { dd = item.DomainAddress })
</div>
}
<div class="item">
@Html.Hidden("OrganizationID", (int)ViewBag.OrganizationID)
</div>
@Html.Partial("_EmailDomainAddPartial", ACD)
</div>
和viewmodel:
public class OrganizationDomainAddressView
{
public OrganizationDomainAddressView(string domainAddress, int organizationId)
{
DomainAddress = domainAddress;
OrganizationID = organizationId;
}
public OrganizationDomainAddressView()
{ }
[AtLeastOneRequired(ErrorMessageResourceType = typeof(AdminResource), ErrorMessageResourceName = "OrganizationEmailDomainRequired")]
[Display(ResourceType = typeof(AdminResource), Name = "OrganizationValidEmailDomain")]
[RegularExpression(@"@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessageResourceType = typeof(AdminResource), ErrorMessageResourceName = "OrganizationEmailFormatError")]
public string DomainAddress { get; set; }
public int OrganizationID { get; set; }
}
代码在控制器上错误输出:
org.DomainAddresses.Remove(new OrganizationDomainAddress(dd.OrganizationID, dd.DomainAddress));
不确定为什么会这样。任何帮助都会很棒!
答案 0 :(得分:1)
根据引发错误的行,检查调试器以下代码部分是否为空:
如果未设置dd,则错误将出现在上方的圆顶线上。根据您的实现,如果其中一个输入值为null,则OrganizationDomainAddress的构造函数可能会抛出NullReferenceException。