ASP。在视图中填充空列表。

时间:2021-07-10 04:05:13

I wish to populate a List<string> object which is completely empty initially but becomes populated in the View.

我希望填充列表 对象,该对象最初完全为空,但在视图中填充。

The object caters for the URL of images on the hard disk so that the system can churn and perform certain logic on them.

该对象跟踪硬盘上图像的URL,这样系统就可以对它们进行修改和执行某些逻辑。

At the moment I am hard-coding the solution and only adding in one string:

目前我正在对解决方案进行硬编码,只添加了一个字符串:

@Html.LabelFor(m => m.ImageUrlCollection[0], "Upload Image")
@Html.TextBoxFor(m => m.ImageUrlCollection[0])

Can anyone see an obvious fix to this? Again, the List is initially empty and I do not have a maximum number for now.

有人能看出一个明显的解决方法吗?同样,列表最初是空的,我现在没有最大编号。

3 个解决方案

#1


2  

I would recommend you taking a look at the following article which illustrates a nice example of how to achieve that. And here's an adaptation for Razor and a main view model instead of working directly with the collection.

我建议您看一下下面的文章,它演示了如何实现这一点的一个很好的例子。这是对Razor的一个改编和一个主要的视图模型,而不是直接使用集合。

#2


0  

As I understand, you trying to populate list with jquery/javascript and then submit form? Look at Darin Dimitrov example. All you need is create inputs with strong names, setting index to each element in name, after form submit you can save populated list in database.

据我所知,您试图用jquery/javascript填充列表,然后提交表单?看看Darin Dimitrov的例子。您所需要的是创建具有强名称的输入,为名称中的每个元素设置索引,在表单提交之后,您可以将填充列表保存到数据库中。

#3


0  

I think you can do the following:

我认为你可以做到以下几点:

In the controller:

控制器:

yourModel.ImageUrlCollection = new List<string>;
//if you want you can yourModel.ImageUrlCollection.Add(new string("First Image"));
View(yourModel);

In your view you do what you do and populate the list the way you want it.

在您的视图中,您执行您所做的操作,并按您希望的方式填充列表。

Please let me know if that helps.

如果有用的话,请告诉我。

#1


2  

I would recommend you taking a look at the following article which illustrates a nice example of how to achieve that. And here's an adaptation for Razor and a main view model instead of working directly with the collection.

我建议您看一下下面的文章,它演示了如何实现这一点的一个很好的例子。这是对Razor的一个改编和一个主要的视图模型,而不是直接使用集合。

#2


0  

As I understand, you trying to populate list with jquery/javascript and then submit form? Look at Darin Dimitrov example. All you need is create inputs with strong names, setting index to each element in name, after form submit you can save populated list in database.

据我所知,您试图用jquery/javascript填充列表,然后提交表单?看看Darin Dimitrov的例子。您所需要的是创建具有强名称的输入,为名称中的每个元素设置索引,在表单提交之后,您可以将填充列表保存到数据库中。

#3


0  

I think you can do the following:

我认为你可以做到以下几点:

In the controller:

控制器:

yourModel.ImageUrlCollection = new List<string>;
//if you want you can yourModel.ImageUrlCollection.Add(new string("First Image"));
View(yourModel);

In your view you do what you do and populate the list the way you want it.

在您的视图中,您执行您所做的操作,并按您希望的方式填充列表。

Please let me know if that helps.

如果有用的话,请告诉我。