Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么是;对象引用未设置为对象的实例。”;在这里提出错误?_C#_Asp.net_.net_Razor - Fatal编程技术网

C# 为什么是;对象引用未设置为对象的实例。”;在这里提出错误?

C# 为什么是;对象引用未设置为对象的实例。”;在这里提出错误?,c#,asp.net,.net,razor,C#,Asp.net,.net,Razor,我正在开发asp.net mvc web应用程序,我有以下观点:- @model MvcApplication4.Models.SelectedCustomers <h3>Select Customers Detials</h3> @using (Html.BeginForm("Export", null)) { <table> <tr> <th> NAME

我正在开发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>
}
四,

::更新:::

我已将我的视图更新为以下内容:-

@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文件将打开,但它将是空的,并且对于每条记录,它将不包含“,”,对于exmaple,如果excel工作表或文本文件中应有7条记录,则.csv文件将具有以下无数据的内容:-

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

问题在于,
IList
不能转换为
IEnumerable
,因此在此处接收
IEnumerable

public ActionResult Export(ExportViewModel exportOptions, IList<AccountDefinition> accounts)
公共操作结果导出(ExportViewModel导出选项,IList帐户) 或者将所有其他内容更改为
IList


另一种方法是:

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)
    );
}
公共操作结果导出(ExportViewModel导出选项,IList帐户) { var enumerableAccounts=accounts.AsEnumerable(); 如果(exportOptions.Format==“csv”) { 返回enumerableAccounts.AsCsvResult(exportOptions); } else if(exportOptions.Format==“xls”) { 返回enumerableAccounts.AsXlsResult(导出选项); } 抛出新的NotSupportedException( Format(“不支持的格式:{0}”,exportOptions.Format) ); } 以下运行(请参见视图中HiddeFor和HiddeFor的区别):

控制器

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);
    }

}
}
namespace SomeApp.Controllers{
公共类TestModel{
公共IEnumerable项{get;set;}
}
公共类TestModelItem{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共类TestsController:控制器{
公共行动结果索引(){
TestModelTM=新的TestModel{
项目=新列表{
新的TestModelItem{Id=1,Name=“first”},
新的TestModelItem{Id=2,Name=“second”}
}
};
返回视图(tm);
}
[HttpPost]
公共行动结果索引(TestModel tm){
返回视图(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>
@model SomeApp.Controllers.TestModel
@{
布局=空;
Int32 c=-1;
}
指数
@foreach(Model.Items中的var i){
C++;
@*@Html.HiddenFor(x=>i.Id)*@
@Hidden(String.Format(“Items[{0}].Id”,c),i.Id)
@DisplayFor(x=>i.Name)
}
去

这似乎意味着此.Accounts为空。请求中没有任何内容允许绑定到IEnumerable帐户。谢谢,你能检查我对原始问题的更新吗?你能发布一行与客户对应的html吗?我重新表述了我的问题:生成的html中隐藏标记的名称是什么。应该选择customers.Info[x].ORG\u ID,其中x是从0到N-1的数字,其中N是客户的编号。@JohnPeter,这是因为您使用的是显示而不是文本框。传输的唯一值是组织ID。您应该在存储之前从ID中恢复数据。感谢回复,我将操作方法更改为recevice IList accounts,但仍然收到以下错误:-“对象引用未设置为对象的实例”。我还尝试了您的操作方法,但是仍然会出现相同的错误。@JohnPeter,实际上,您需要从操作方法一直到下一步都使用相同的类型。因此,将action方法中接收到的任何内容转换为
IEnumerable
,其他内容保持原样。根据你的两条评论,感觉堆栈的其他部分现在也发生了一些变化。我敢肯定,自从你问起,你一直在尝试不同的事情。@JohnPeter,我敢肯定,如果你在扩展方法中检查了
accounts
的值,它是
null
-你能验证一下吗?嗯。。。
IList
如何不能转换为
IEnumerable
IList
继承自
IEnumerable
。thnak您可以检查我的:::更新:::部分文件现在将打开,但没有数据。
 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)
            );
        }
, , 
, , 
, , 
, , 
, , 
, , 
, , 
public ActionResult Export(ExportViewModel exportOptions, IList<AccountDefinition> accounts)
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)
    );
}
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>