将列表 <string> 从控制器传递到视图 ASP.NET(空引用)
Passing List<string> from controller to View ASP.NET (null reference)
我有一个class。在此 class 中,我想列出文件夹中我的 .txt 文件的名称并将它们保存到 List<string>
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace Aplikacja.Models
{
public class Lists
{
public List<string> ListFiles()
{
DirectoryInfo dir = new DirectoryInfo(@"C:\Users\Kamm\Documents\ASP.NET\");
FileInfo[] files = dir.GetFiles("*.txt");
string str = "";
List<string> allfiles = new List<string>();
foreach(FileInfo file in files)
{
str = file.Name;
allfiles.Add(str);
}
return allfiles;
}
}
}
然后,我有一个控制器将值放入列表以将其传递给视图:
[HttpPost]
public ActionResult Index(string listButton)
{
if (listButton != null)
{
var list = new Lists();
list.ListFiles();
var names = new List<string>();
names = list.ListFiles();
ViewBag.List = names;
return View();
}
}
我有一个视图来填充列表:
<button value="List" name="listButton" type="submit" class="btn btn-primary" formmethod="post">List</button>
<ul>
@foreach (var item in (List<string>)ViewBag.List){
<span>
@item
</span>
}
</ul>
但是当我启动应用程序时,出现错误:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 12: <button value="List" name="listButton" type="submit" class="btn btn-primary" formmethod="post">List</button>
Line 13: <ul>
Line 14: @foreach (var item in (List<string>)ViewBag.List){
Line 15: <span>
Line 16: @item
Source File: c:\Users\Kamm\Documents\ASP.NET\Aplikacja\Aplikacja\Aplikacja\Views\Home\Index.cshtml Line: 14
有人可以帮助我吗?我不知道该怎么做,即使我只想从 Controller 传递一个简单的列表,我也会遇到同样的错误。
已解决!
我在视图中添加了一行:
@if ((List<string>)ViewBag.List != null)
可能是应用程序启动时出了点问题..
您的代码有很多问题,但是出现该异常的原因是您没有在控制器的 HTTP GET 操作中将文件名列表添加到 VieBag,而是尝试在HTTP POST 操作。
在 Index 操作方法上将 [HttpPost] 更改为 [HttpGet] 或者只删除该属性(控制器中的每个操作都是 HttpGet默认情况下,除非你用其他属性标记它)并且它会起作用。
[HttpGet]
public ActionResult Index()
{
var lists = new Lists();
var names = lists.ListFiles();
ViewBag.List = names;
return View();
}
但是,更简洁的解决方案是使您的视图成为强类型的,并且 return 来自您的操作方法的文件名列表。
在控制器中使用:
Return 查看(姓名);
在您的视图顶部包括您要发送的模型:
@model 列表
并在循环中使用:
@foreach(模型中的变量项)...
始终检查对象是否为空。
这里;检查 ViewBag.List 是否为空。
您可以在后面编写代码或在发现时使用
@if ((列表)ViewBag.List != null)
我有一个class。在此 class 中,我想列出文件夹中我的 .txt 文件的名称并将它们保存到 List<string>
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace Aplikacja.Models
{
public class Lists
{
public List<string> ListFiles()
{
DirectoryInfo dir = new DirectoryInfo(@"C:\Users\Kamm\Documents\ASP.NET\");
FileInfo[] files = dir.GetFiles("*.txt");
string str = "";
List<string> allfiles = new List<string>();
foreach(FileInfo file in files)
{
str = file.Name;
allfiles.Add(str);
}
return allfiles;
}
}
}
然后,我有一个控制器将值放入列表以将其传递给视图:
[HttpPost]
public ActionResult Index(string listButton)
{
if (listButton != null)
{
var list = new Lists();
list.ListFiles();
var names = new List<string>();
names = list.ListFiles();
ViewBag.List = names;
return View();
}
}
我有一个视图来填充列表:
<button value="List" name="listButton" type="submit" class="btn btn-primary" formmethod="post">List</button>
<ul>
@foreach (var item in (List<string>)ViewBag.List){
<span>
@item
</span>
}
</ul>
但是当我启动应用程序时,出现错误:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 12: <button value="List" name="listButton" type="submit" class="btn btn-primary" formmethod="post">List</button>
Line 13: <ul>
Line 14: @foreach (var item in (List<string>)ViewBag.List){
Line 15: <span>
Line 16: @item
Source File: c:\Users\Kamm\Documents\ASP.NET\Aplikacja\Aplikacja\Aplikacja\Views\Home\Index.cshtml Line: 14
有人可以帮助我吗?我不知道该怎么做,即使我只想从 Controller 传递一个简单的列表,我也会遇到同样的错误。
已解决! 我在视图中添加了一行:
@if ((List<string>)ViewBag.List != null)
可能是应用程序启动时出了点问题..
您的代码有很多问题,但是出现该异常的原因是您没有在控制器的 HTTP GET 操作中将文件名列表添加到 VieBag,而是尝试在HTTP POST 操作。
在 Index 操作方法上将 [HttpPost] 更改为 [HttpGet] 或者只删除该属性(控制器中的每个操作都是 HttpGet默认情况下,除非你用其他属性标记它)并且它会起作用。
[HttpGet]
public ActionResult Index()
{
var lists = new Lists();
var names = lists.ListFiles();
ViewBag.List = names;
return View();
}
但是,更简洁的解决方案是使您的视图成为强类型的,并且 return 来自您的操作方法的文件名列表。
在控制器中使用: Return 查看(姓名);
在您的视图顶部包括您要发送的模型:
@model 列表
并在循环中使用:
@foreach(模型中的变量项)...
始终检查对象是否为空。
这里;检查 ViewBag.List 是否为空。 您可以在后面编写代码或在发现时使用 @if ((列表)ViewBag.List != null)