-1

I struggled a lot with this today, I hope this post helps other people

The case

I have a form, which must contain dynamic amount of fields, depending on user input. The model for this form is Contest:

public int NumberOfRounds { get; set; }
public List<Round> Rounds { get; set; }

Round model looks like this:

public int Length { get; set; }

NumberOfRounds field indicates the amount of Rounds a Contest contains. I use a simple js script with "change" event listener to add additional input fields, according to the value of NumberOfRounds entered by the user.

The question is how to bind those input fields to my Contest model, in order to get their values in inside the Round.Length property.

Community
  • 1
  • 1
Alex
  • 715
  • 1
  • 8
  • 29
  • I disagree that this is a duplicate. Even if so, the other post is way too lengthily and uses partial views. I simply wanted to touch on a topic on which I could not find a simple solution. From the point of view of a complete ASP.NET noob, the other topic is not very helpful. The fact that the problem is touched in another topic, does not mean information redundancy. If you insist, however, I can remove this post. – Alex Feb 27 '18 at 12:01
  • @StephenMuecke explain, please. – Alex Feb 27 '18 at 17:58
  • Explain what? You have asked a question and I duped it to an answer which explains options for how to resolve your question, and explains the benefits and limitations of each (and only one of the options is related to a partial view). It not only explains how to correctly generate the html, but also explains how to resolve other essential issues such as client side validation, and adding the input for the collection indexer, which your own answer does not even touch on. –  Feb 28 '18 at 01:05
  • @StephenMuecke Yes, my answer also does not include lecture on Nuclear physics. Is that a problem? I asked a concrete question and gave a concrete answer, with a clear example. The goal is to be easily readable from a new-comer's perspective. A simple problem with simple resolution and a simple topic. I am going to say this again reading the original of my so called "duplicate" can give me a headache even after I figured out the solution. I can see how it is relevant, but exact duplicate? I don't think so. – Alex Feb 28 '18 at 16:56
  • And your answer is a simple resolution''. But now users seeing this can see a more comprehensive answer that not only explains the issue in detail, but also shows how to do it correctly. If you see that as a problem, then I can only assume the question/answer was for posted solely for gaining rep, and not helping others. –  Mar 01 '18 at 06:39
  • @StephenMuecke I don't care about reputation. However I thought that you voted down my question, but maybe I assumed wrong. Also aren't **duplicate** questions considered as bad? Since the user didn't research properly and such? – Alex Mar 01 '18 at 11:56
  • 1
    No and No. Duplicates are not bad (except for the trivial cases where even the most basic google would have given the answer) - they help users to find other related answers that will be helpful. –  Mar 01 '18 at 12:09

1 Answers1

0

The key is in the name attribute of the HTML tag. At the top of my View, I have defined @model Contest. This sets the context of the View to the Contest object. Now we can use name attribute to bind an input value to the corresponding model property.

I recommend this article as a good read on Model binding to a List property. Also this topic for more specific information.

In our case however we have a List<Round> Rounds property. How do we bind individual inputs to each Round.Length. Its simply:

<input ... name="Rounds[{index}].Length"

Where {index} is the position at the Rounds list, at which to save the current input's value. True, it is simple enough, but I think a clear example of the issue would be very useful for newcomers. Hope it helps :)

Alex
  • 715
  • 1
  • 8
  • 29