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

KyleMit
  • 30,350
  • 66
  • 462
  • 664
The First
  • 139
  • 2
  • 13
  • 2
    You've got the ` – Reinstate Monica Cellio Sep 06 '18 at 11:56
  • @Archer let me try , just sec :) – The First Sep 06 '18 at 11:58
  • 2
    Why are you not just using the `@Html.DropDownListFor()` method? –  Sep 06 '18 at 12:05
  • @Archer you were right , i did miss dat :) but when i use like this in my if i will get error operator '==' cannot be applied to operands of type 'list' and 'string' if (Model.OrdreRMAs.SerialNoInvoiceOrdrelineDeliveryClose == "") , and then i use string.IsNullOrEmpty and i also used != but its same – The First Sep 06 '18 at 12:05
  • Hi @StephenMuecke , would you please tell me what i need to using with @Html.DropDownListFor() or an example if it possible :) – The First Sep 06 '18 at 12:08
  • 1
    Refer the code in [this question](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) 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) –  Sep 06 '18 at 12:14
  • @StephenMuecke :) the only problem i have with generating Html helpers in views its only DropDownList , but now i get useful link from you where i can working on my problem with DropDownList. – The First Sep 06 '18 at 12:20
  • @StephenMuecke Can i ask one more question ? – The First Sep 06 '18 at 12:21
  • 1
    If you mean the other error - then you use `if (Model.OrdreRMAs.SerialNoInvoiceOrdrelineDeliveryClose.Any()) { @Html.DropDownListFor(...) } else { @Html.TextBoxFor(...) }` –  Sep 06 '18 at 12:22
  • @StephenMuecke Yes , thank you so much for help :) – The First Sep 06 '18 at 12:32
  • @StephenMuecke Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179549/discussion-between-the-first-and-stephen-muecke). – The First Sep 06 '18 at 12:52

3 Answers3

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>
Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
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>
Dmitry Korolev
  • 675
  • 4
  • 20
0

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

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

KyleMit
  • 30,350
  • 66
  • 462
  • 664
sapana
  • 46
  • 6