我不确定为什么会收到此错误:
你调用的对象是空的。
我有一个数据库,里面有一些组织,每个组织可以有多个域。我设置它的方式是只有一个常规的 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>
和视图模型:
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));
不知道为什么会这样。任何帮助都会很棒!