0

I'm fairly new to MVC and I wondered if something along the following lines is possible?

I'm trying to create html helpers for textboxes for new items in a list. The amount of items in the list is dynamic and unknown.

Model (person.cs)

public class Person
{
    public List<string> Strings = new List<string>();
}

View (Index.cshtml)

@model Project.Models.Person

@{

    int length = (int)Session["Length"];

    for (int i = 0; i < length; i++)
    {
        Html.TextBoxFor(p => p.Strings[i])
    }
}

Obviously this code does not work, but it is the simplest representation of what I want to achieve, and I was wondering what I can do to make this code work, or any workarounds to allow me to dynamically add or create new items in a collection.

ekad
  • 14,436
  • 26
  • 44
  • 46
Zollistic
  • 98
  • 1
  • 8
  • Some options for dynamically adding and deleting collection items [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) –  Apr 08 '15 at 13:08
  • http://stackoverflow.com/questions/9385797/loop-through-model-and-create-textbox-for-each-property-in-the-view – Pseudonym Apr 08 '15 at 13:17

2 Answers2

1

Use ViewBag. When returning View from controller

   ViewBag.Strings = string.Join(", ", Strings .Select(x => x.Name));

In the view,

@foreach (var str in ((List<string>)ViewBag.Strings)) 
{
  @str //In your case textbox
}
Chatra
  • 2,989
  • 7
  • 40
  • 73
0

It is good practice to avoid using session states in MVC projects. MVC is structured around the idea that your website is a view into a logical model of information. It encourages having stateless operations through the use of simple controllers responding to actions with key information passed as part of the HTTP request.

This following code snippet will display a input control for each item in the string collection.

@model Project.Models.Person

@{
    for (int i = 0; i < Model.Strings.Count; i++)
    {
        @Html.TextBox((string.Format("[{0}]", i), Model.Strings[i]);
    }
}
user2818985
  • 380
  • 3
  • 10
  • 1
    Which would generate inputs with name attributes that have absolutely no relationship to the model and would not bind to the model on post back –  Apr 08 '15 at 13:56
  • I agree, if the intention is to post back to the Controller I suggest using a Method that look like the following Index(List data) and use the index enclosed within square brackets as the input element name e.g. [0], [1], [3] etc – user2818985 Apr 09 '15 at 11:13
  • Still does not work. It needs to be `@Html.TextBoxFor(m => m.Strings[i])` to generate the correct name attribute. But this does not address OP's question which is to dynamically add new items to the list (this just shows how to render existing ones) –  Apr 09 '15 at 11:23
  • Sorry about the delay, I have been away on my hols. The HTML Helper will not work because Editor For method does not know how to assign a name to each item in the collection. However, you can provide an implicit name which can be assigned to the Action by the Model Binder. If you need more information you should see [link](http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/) – user2818985 Apr 22 '15 at 21:45