Subby
Subby

Reputation: 5480

ASP.NET MVC Populating Empty List<string> in View

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.

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.

Upvotes: 2

Views: 3033

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

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.

Upvotes: 2

Display Name
Display Name

Reputation: 4732

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.

Upvotes: 0

webdeveloper
webdeveloper

Reputation: 17288

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.

Upvotes: 0

Related Questions