为什么“对象引用”没有设置为对象的实例?这里出现错误

本文关键字:对象引用 实例 这里 错误 为什么 设置 对象 | 更新日期: 2023-09-27 17:49:29

我正在一个asp.net mvc web应用程序工作,我有以下视图:-

@model MvcApplication4.Models.SelectedCustomers
<h3>Select Customers Detials</h3>
@using (Html.BeginForm("Export", null))
{    <table>
        <tr>
            <th>
                NAME @Html.CheckBox("IncludeName", true)
            </th>
            <th>
                Description @Html.CheckBox("IncludeDescription", true)
            </th>
            <th>
                Address @Html.CheckBox("IncludeAddress", true)
            </th>
        </tr>
        @foreach (var item in Model.Info) {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.ORG_NAME)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.LOGIN_URI)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.SUPPORT_EMAIL)
                </td>
                @Html.HiddenFor(modelItem => item.ORG_ID)
            </tr>
        }
    </table>
    <p>
        @Html.ActionLink("Back","customer","Home") |
        <button type="submit" name="Format" value="xls">extract to excel</button> |
        <button type="submit" name="Format" value="csv">extract to text file</button>
    </p>
}

和以下动作方法:-

[HttpPost]
        public ActionResult Export(ExportViewModel exportOptions, IList<AccountDefinition> accounts)
        {
          // var accounts = GetAccount();
            if (exportOptions.Format == "csv")
            {
                return accounts.AsCsvResult(exportOptions);
            }
            else if (exportOptions.Format == "xls")
            {
                return accounts.AsXlsResult(exportOptions);
            }
            throw new NotSupportedException(
                string.Format("Unsupported format: {0}", exportOptions.Format)
            );
        }

然后我在我的AccountDefinition模型中定义了以下方法:-

 public partial class AccountDefinition
    {
        public long ORG_ID { get; set; }
        public string ORG_NAME { get; set; }
        public string LOGIN_URI { get; set; }
        public string SUPPORT_EMAIL { get; set; }
        public virtual SDOrganization SDOrganization { get; set; }
    }
    public static class ActionResultextensions
    {
        public static ActionResult AsCsvResult(this IEnumerable<AccountDefinition> accounts, ExportViewModel exportOptions)
        {
            return new CsvResult(accounts, exportOptions);
        }
        public static ActionResult AsXlsResult(this IEnumerable<AccountDefinition> accounts, ExportViewModel exportOptions)
        {
            return new XlsResult(accounts, exportOptions);
        }
    }
}

然后我有以下模型类:-

1。

public class CsvResult : ExportAccountsResult
    {
        public CsvResult(IEnumerable<AccountDefinition> accounts, ExportViewModel exportOptions)
            : base(accounts, exportOptions)
        {
        }
        protected override string ContentType
        {
            get { return "text/csv"; }
        }
        protected override string Filename
        {
            get { return "accounts.csv"; }
        }        }

2,

 public class XlsResult : ExportAccountsResult
    {
        public XlsResult(IEnumerable<AccountDefinition> accounts, ExportViewModel exportOptions)
            : base(accounts, exportOptions)
        {
        }
        protected override string ContentType
        {
            get { return "application/vnd.ms-excel"; }
        }
        protected override string Filename
        {
            get { return "accounts.csv"; }
        }
    }

3。

public class ExportViewModel
    {
        public string Format { get; set; }
        public bool IncludeName { get; set; }
        public bool IncludeDescription { get; set; }
        public bool IncludeAddress { get; set; }
    }

4。

public abstract class ExportAccountsResult : ActionResult
    {
        protected ExportAccountsResult(IEnumerable<AccountDefinition> accounts, ExportViewModel exportOptions)
        {
            this.Accounts = accounts;
            this.ExportOptions = exportOptions;
        }
        protected IEnumerable<AccountDefinition> Accounts { get; private set; }
        protected ExportViewModel ExportOptions { get; private set; }
        protected abstract string ContentType { get; }
        protected abstract string Filename { get; }
        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.HttpContext.Response;
            response.ContentType = ContentType;
            var cd = new ContentDisposition
            {
                FileName = this.Filename,
                Inline = false
            };
            response.AddHeader("Content-Disposition", cd.ToString());
            // TODO: Use a real CSV parser here such as https://github.com/JoshClose/CsvHelper/wiki/Basics
            // and never roll your own parser as shown in this oversimplified
            // example. Here's why: http://secretgeek.net/csv_trouble.asp
            using (var writer = new StreamWriter(response.OutputStream))
            {
                foreach (var account in this.Accounts)
                {
                    var values = new List<object>();
                    if (this.ExportOptions.IncludeName)
                    {
                        values.Add(account.ORG_NAME);
                    }
                    if (this.ExportOptions.IncludeDescription)
                    {
                        values.Add(account.LOGIN_URI);
                    }
                    if (this.ExportOptions.IncludeAddress)
                    {
                        values.Add(account.SUPPORT_EMAIL);
                    }
                    writer.WriteLine(string.Join(", ", values));
                }
            }
        }
    }

但是当我运行点击"提取到excel"或"提取到文本文件"按钮内的视图,我得到以下例外:-

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=MvcApplication4
  StackTrace:
       at MvcApplication4.Models.ExportAccountsResult.ExecuteResult(ControllerContext context) in c:'Users'Administrator'Desktop'MvcApplication4'MvcApplication4'Models'ExportAccountsResult.cs:line 41
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
  InnerException:

对ExportAccountsresults.cs中的以下代码进行修改:-

 foreach (var account in this.Accounts)
                {
更新

::::::

我已经更新了我的观点如下:-

@model MvcApplication4.Models.SelectedCustomers

<h3>Select Customers Detials</h3>
    @using (Html.BeginForm("Export", null))
    {
        Int32 c = -1;
        <table>
            <tr>
                <th>
                    NAME @Html.CheckBox("IncludeName", true)
                </th>
                <th>
                    Description @Html.CheckBox("IncludeDescription", true)
                </th>
                <th>
                    Address @Html.CheckBox("IncludeAddress", true)
                </th>
            </tr>
            @foreach (var item in Model.Info) {
                c++;
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.ORG_NAME)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.LOGIN_URI)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.SUPPORT_EMAIL)
                    </td>
                     <td>@Html.Hidden(String.Format("Info[{0}].ORG_ID", c), item.ORG_ID)</td>
                </tr>
            }
        </table>
        <p>
            @Html.ActionLink("Back","customer","Home") |
            <button type="submit" name="Format" value="xls">extract to excel</button> |
            <button type="submit" name="Format" value="csv">extract to text file</button>
        </p>
    }

和我的动作方法到下面:-

  [HttpPost]
        public ActionResult Export(ExportViewModel exportOptions, SelectedCustomers sc)
        {
          // var accounts = GetAccount();
           var accounts = sc.Info.ToList();
            if (exportOptions.Format == "csv")
            {
                return accounts.AsCsvResult(exportOptions);
            }
            else if (exportOptions.Format == "xls")
            {
                return accounts.AsXlsResult(exportOptions);
            }
            throw new NotSupportedException(
                string.Format("Unsupported format: {0}", exportOptions.Format)
            );
        }

现在csv文件将打开,但它将是空的,它将只包含",,"每条记录,例如,如果在excel工作表或文本文件中应该有7条记录,那么。csv文件将有以下没有数据的内容:-

, , 
, , 
, , 
, , 
, , 
, , 
, , 

为什么“对象引用”没有设置为对象的实例?这里出现错误

下面运行(请在视图中查看HiddeFor和Hidden的区别):

控制器

namespace SomeApp.Controllers {
public class TestModel {        
    public IEnumerable<TestModelItem> Items { get; set; }
}
public class TestModelItem {
    public int Id { get; set; }
    public String Name { get; set; }
}
public class TestsController : Controller {
    public ActionResult Index() {
        TestModel tm = new TestModel {
            Items = new List<TestModelItem> {
                new TestModelItem { Id = 1, Name = "first"},
                new TestModelItem { Id = 2, Name = "second"}
            }
        };
        return View(tm);
    }
    [HttpPost]
    public ActionResult Index(TestModel tm) {
        return View(tm);
    }
}
}

和视图

@model SomeApp.Controllers.TestModel
@{
    Layout = null;
    Int32 c = -1;
}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    <form method="post">
        <table>
        @foreach (var i in Model.Items) {
            c++;
            <tr>
                @*<td>@Html.HiddenFor(x => i.Id)</td>*@
                <td>@Html.Hidden(String.Format("Items[{0}].Id", c), i.Id)</td>
                <td>@Html.DisplayFor(x => i.Name)</td>
            </tr>
        }
        </table>
        <button type=submit>go</button>
    </form>
</body>
</html>

问题是IList<AccountDefinition>不能转换为IEnumerable<AccountDefinition>,因此在这里接收IEnumerable<T>:

public ActionResult Export(ExportViewModel exportOptions, IList<AccountDefinition> accounts)

或将其他所有内容更改为IList<T>


另一个方法是:

public ActionResult Export(ExportViewModel exportOptions, IList<AccountDefinition> accounts)
{
    var enumerableAccounts = accounts.AsEnumerable();
    if (exportOptions.Format == "csv")
    {
        return enumerableAccounts.AsCsvResult(exportOptions);
    }
    else if (exportOptions.Format == "xls")
    {
        return enumerableAccounts.AsXlsResult(exportOptions);
    }
    throw new NotSupportedException(
        string.Format("Unsupported format: {0}", exportOptions.Format)
    );
}