2

I want to print all Records at once, and for this I used String.Join(). However, the problem is I cant make New Line when I want print all records in DropDownList and as you see in Screenshots. Instead, it select all at once, but I want select one-by-one, like normal DropDownList.

Can anyone please help me or point me in the right direction at what did I wrong?

Here's what I have so far

ViewModel:

public OrdreRMA OrdreRMAs { get; set; }

public class OrdreRMA
{
  public OrdreRMA(List<string> SerialNoInvoiceOrdrelineDeliveryClose)
    {
        this.SerialNoInvoiceOrdrelineDeliveryClose = SerialNoInvoiceOrdrelineDeliveryClose;

    }

    public List<string> SerialNoInvoiceOrdrelineDeliveryClose { get; set; }

}

Controller:

Serial = data.Item_Ledger_Entry
  .Where(ledger => ledger.Document_No_ == t.Document_No_)
  .Where(ledger => ledger.Document_Line_No_ == t.Line_No_)
  .ToList(),
        
var bla4 = col2.Select(t => new OrdreRMA
{
 SerialNoInvoiceOrdrelineDeliveryClose = t.Serial.Select(x => x.Serial_No_).ToList(),
}

1.View (First I used , ):

@{
  var SerialNos = 
    String.Join(",",Model.OrdreRMAs.SerialNoInvoiceOrdrelineDeliveryClose);  
    }

    <div class="col-md-3">
      <div class="form-group">
      <label>Serial number</label>
    @if (SerialNos == "")
    {
      <input name="ikketilgængelig" id="ikketilgængelig" class="form-control border-input disabled"  value="not available">
    }

  else
  {
      <select class="form-control border-input" id="ddlSerial">
      <option value="@SerialNos">@SerialNos</option>
      </select>
    }
  </div>
</div>

Result 1:

result 1

2.View (Second I used Environment.NewLine):

@{
  var SerialNos = 
    String.Join(Environment.NewLine,Model.OrdreRMAs.SerialNoInvoiceOrdrelineDeliveryClose);  
    }

   <div class="col-md-3">
     <div class="form-group">
      <label>Serial number</label>
   @if (SerialNos == "")
    {
     <input name="ikketilgængelig" id="ikketilgængelig" class="form-control border-input disabled"  value="not available">
    }

 else
  {
     <select class="form-control border-input" id="ddlSerial">
     <option value="@SerialNos">@SerialNos</option>
     </select>
    }
  </div>
</div>

**Result 2**: 

[![Result #2][2]][2]

**3.View (instead using `String.Join`, I used `Foreach`)**:

```html
   <div class="col-md-3">
     <div class="form-group">
      <label>Serial number</label>
   @if (SerialNos == "")
    {
     <input name="ikketilgængelig" id="ikketilgængelig" class="form-control border-input disabled"  value="not available">
    }

 else
  {
    @foreach (var item in Model.OrdreRMAs.SerialNoInvoiceOrdrelineDeliveryClose)
     {
  <select class="form-control border-input" id="ddlSerial">
   <option value="@item">@item</option>
   </select>
    }
 </div>
  }
  </div>
</div>

Result 3:

Result 3

11
  • 2
    You've got the <select> tags inside the loop. Put them outside so you only add options in the loop. Commented Sep 6, 2018 at 11:56
  • @Archer let me try , just sec :) Commented Sep 6, 2018 at 11:58
  • 2
    Why are you not just using the @Html.DropDownListFor() method? Commented Sep 6, 2018 at 12:05
  • 1
    Refer the code in this question for a typical example (and I suggest you go to the mvc site and work through the tutorials to learn the basics of generating views in mvc) Commented Sep 6, 2018 at 12:14
  • 1
    If you mean the other error - then you use if (Model.OrdreRMAs.SerialNoInvoiceOrdrelineDeliveryClose.Any()) { @Html.DropDownListFor(...) } else { @Html.TextBoxFor(...) } Commented Sep 6, 2018 at 12:22

3 Answers 3

1

Try to put select out side foreach loop

<select class="form-control border-input" id="ddlSerial">
 @foreach (var item in Model.OrdreRMAs.SerialNoInvoiceOrdrelineDeliveryClose)
 {
       <option value="@item">@item</option>     
  }
</select>
Sign up to request clarification or add additional context in comments.

Comments

1

You are putting foreach in a wrong place.

<select class="form-control border-input" id="ddlSerial">
@foreach (var item in Model.OrdreRMAs.SerialNoInvoiceOrdrelineDeliveryClose)
{  
     <option value="@item">@item</option>
}
</select>

Comments

0

How about making an array of List<string> in controller.

And later using it as @String.Join(",", @item.arrayname) in Razor?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.