简体   繁体   English

如何使用 Razor 在 C# 中传递数组

[英]How do you pass an array in C# using Razor Pages

This is my viewtrainees.cshtml.cs file snippets that contain the array code:这是包含数组代码的 viewtrainees.cshtml.cs 文件片段:

public class viewTraineeModel : PageModel
{
    public string[] traineeinfo { get; set; }
    (...)

In my OnGet():在我的 OnGet() 中:

string[] traineeinfo = new string[Convert.ToInt32(countnum)];

int i = 0;

while (trainees.Read())
{
    traineeinfo[i] = trainees["t3_win"].ToString();
    i++;
}

So everything is set in my array fine, I even tested it within the.cs file.所以一切都在我的数组中设置好了,我什至在 .cs 文件中对其进行了测试。 However, when I pass the array or think I pass the array to the view it ALWAYS gives me an internal error.但是,当我传递数组或认为我将数组传递给视图时,它总是会给我一个内部错误。

I have tried on my viewtrainees.cshtml file:我已经尝试过我的 viewtrainees.cshtml 文件:

@Model.traineeinfo[0]

And I get NullReferenceException: Object reference not set to an instance of an object.我得到 NullReferenceException: Object 引用未设置为 object 的实例。 Error.错误。

The head element of my code looks like this.我的代码的 head 元素看起来像这样。

@page
@model T3_Landing.Pages.viewTraineeModel

@{
    ViewData["Title"] = "View Trainees";

    string[] traineeinfo = Model.traineeinfo as string[];    
}

I have tried it with and without re-initializing the string array as seen above.如上所示,我已经尝试过重新初始化字符串数组和不重新初始化字符串数组。 I really want this to work.我真的希望这个工作。

Just a note I can pass regular strings just fine using @Model.examplestring but never an array, list, or object.请注意,我可以使用@Model.examplestring 很好地传递常规字符串,但不能传递数组、列表或 object。

It's hard to tell without the full context, but it looks like you're hiding the class member by declaring a local variable within OnGet() :没有完整的上下文很难判断,但看起来你通过在OnGet()中声明一个局部变量来隐藏class 成员:

string[] traineeinfo = new string[Convert.ToInt32(countnum)];

This creates a local variable named traineeinfo that hides the class property (if that's the case I would expect a compiler warning).这将创建一个名为traineeinfo局部变量,该变量隐藏了 class 属性(如果是这种情况,我会收到编译器警告)。 Since the class property is not set, you get a null reference exception in your Razor page.由于未设置 class 属性,因此在 Razor 页面中会出现 null 引用异常。

Try just试一试

traineeinfo = new string[Convert.ToInt32(countnum)];

I would also recommend using camel case to make it easier to read:我还建议使用驼峰式案例以使其更易于阅读:

public string[] TraineeInfo { get; set; }

....

TraineeInfo = new string[Convert.ToInt32(countnum)];

Here on constructor, I think you may initialize string array for prevent null value.在构造函数上,我认为您可以初始化字符串数组以防止 null 值。

public class viewTraineeModel : PageModel
{
public string[] traineeinfo { get; set; }
...

public viewTraineeModel()
{
traineeinfo=new string[0];
}

Please drop a screen shot or code snippet of how you're passing the data to your view from your controller method end.请从 controller 方法结束将数据传递到视图的屏幕截图或代码片段。 That will determine if you have the data you want to work with in your view in the first place.这将首先确定您的视图中是否有要使用的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM