0

I have a drop-down list (where multiple options can be selected) and two input boxes inside another div with id 'showInvestmentForm'.Upon selection of drop-down items,I want to produce those text boxes with respect to the number of drop-down items selected. I want these text boxes to have different Names but I cant find a way to provide them different Names in such a way that their input values get passed to the controller.

This is a part of my Controller:-

foreach(var data in propertyViewModel.PropertyInvestors)
    {
    FounderInvestment founderInvestment = new FounderInvestment
    {
        Id = propertyViewModel.FounderInvestmentViewModel.Id ?? 0,
        InstallmentPeriod = propertyViewModel.FounderInvestmentViewModel.InstallmentPeriod,
        InvestorId = Convert.ToInt32(data),
        PropertyId = property.Id,
        Investment = propertyViewModel.FounderInvestmentViewModel.Investment
    };
    _founderInvestmentQueryProcessor.Create(founderInvestment);
}

This is my dropdown:-

<div class="form-group">
  @Html.LabelFor(model => model.PropertyInvestors, htmlAttributes: new { @class = "control-label col-md-2" })
  <div class="col-md-10">
    @Html.DropDownListFor(model => model.PropertyInvestors, (IEnumerable<SelectListItem>)@ViewBag.Investors, "Select", htmlAttributes: new {@id="dropdown", @class = "form-control",@multiple="multiple" })
    @Html.ValidationMessageFor(model => model.PropertyInvestors, "", new { @class = "text-danger" })
  </div>
</div>

These are the textboxes:-

<div id="showInvestmentForm" style="display:none">                        
    <div class="form-group">
    @Html.LabelFor(model => model.FounderInvestmentViewModel.Investment, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
        @Html.EditorFor(model => model.FounderInvestmentViewModel.Investment, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.FounderInvestmentViewModel.Investment, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.FounderInvestmentViewModel.InstallmentPeriod, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FounderInvestmentViewModel.InstallmentPeriod, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.FounderInvestmentViewModel.Investment, "", new { @class = "text-danger" })
        </div>
    </div>                       
</div>
Sudeep Shrestha
  • 128
  • 2
  • 14
SudeepS
  • 29
  • 9
  • I'll roll it back and add an answer, but it will not be until tomorrow. –  Dec 20 '17 at 10:45
  • Ok.Thank You so so so very much.You have helped me so many times. :D – SudeepS Dec 20 '17 at 10:50
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/161618/discussion-on-question-by-sudeep-shrestha-how-to-produce-n-no-of-text-boxes-h). – Bhargav Rao Dec 20 '17 at 12:18
  • I understand that your conversation had ended, but as you've mentioned that you are going to be adding an answer tomorrow, I've moved it to chat from where you can check back your conversation and compile your answer. – Bhargav Rao Dec 20 '17 at 12:19

1 Answers1

2

If I understood correctly your problem is that input texts are not posted to your Controller. Would be usefult if you write the Controller's code to check the full scenario.

Anyway a common mistake, that could be your case, when using Mvc helpers is that when you point to a nested property, the generated name of the input is not the final property name but a path to that property.

@Html.EditorFor(model => model.FounderInvestmentViewModel.InstallmentPeriod, ..)

In your case that code produce an input with name FounderInvestmentViewModel_InstallmentPeriod, and not just InstallmentPeriod.

If in your controller you expect a parameter named "InstallmentPeriod" that would not be received, because the page posts a parameter named "FounderInvestmentViewModel_InstallmentPeriod"

Luca Corradi
  • 2,031
  • 1
  • 16
  • 17
  • I understand what You mean.But right now what I am trying to ask is that how do i produce multiple textboxes with respect to the number of dropdown items selected and these texboxes should have different ids and names but should be able to pass their input value to the controller. For eg:- If I select total of 3 items in the drop down, '#showInvestmentform' should be produced 3 times. Right now with the JS code that I am working on produces the desired number of textboxes but when I pass it to the controller,because of the same id and name, SAME input Texts are being passed. – SudeepS Dec 20 '17 at 08:37
  • This has nothing at all to do with OP's issue. –  Dec 20 '17 at 08:37
  • I hope i got my problem through to You. :/ I am stuck on this problem for a while now. – SudeepS Dec 20 '17 at 08:37
  • I suggest you to insert a client-side Javascript code that generates the inputs. You could name those inputs with this name format: 1st input of the 1st group --> inputInvestments[0].InstallmentPeriod 2nd input of the 1st group --> inputInvestments[0].Investment || 1st input of the 2nd group --> inputInvestments[1].InstallmentPeriod || 2nd input of the 2nd group --> inputInvestments[1].Investment || And so on... On the Controller's side you can have an action that takes as parameter a IEnumerable named inputInvestments – Luca Corradi Dec 20 '17 at 08:49
  • Who keeps up-voting this nonsense! It has nothing to do with the question. –  Dec 20 '17 at 09:19