This is a migrated thread and some comments may be shown as answers.

DropDownList Inside An MVC If Statement

1 Answer 467 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Graham
Top achievements
Rank 2
Iron
Iron
Graham asked on 05 Nov 2018, 03:41 PM

My dropdownlist is absolutely fine until if surround it with an MVC if statement. It then becomes invisible, while the text around it is visible.

Before Adding If

  <b>Central Hub</b>
         
  @(Html.Kendo().DropDownList()
      .Name("postcodeServiceExclusionsHubSelector")
      .HtmlAttributes(new { style = "width: 70px;" })
      .DataTextField("DepotNumber")
      .DataValueField("DepotID")
      .BindTo(Model.CentralHubs)
      .OptionLabel("Select...")
)

After Adding MVC If Statement

@if (Model.CanSelectCentralHub)
{
    @:<b>Central Hub</b>
    @:      
    Html.Kendo().DropDownList()
        .Name("postcodeServiceExclusionsHubSelector")
        .HtmlAttributes(new { style = "width: 70px;" })
        .DataTextField("DepotNumber")
        .DataValueField("DepotID")
        .BindTo(Model.CentralHubs)
        .OptionLabel("Select...");
     
    @:            
}

 

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 07 Nov 2018, 08:27 AM
Hello Graham,

You should be able to workaround this by surrounding the DropDownList initialization with @( ):
@if (true)
{
  @:<b>Central Hub</b>
  @:
  @(Html.Kendo().DropDownListFor(m => m.ClassId)
    .DataTextField("ProductName")
    .DataValueField("ProductID")
    .HtmlAttributes(new { style = "width:100%" })               
    .DataSource(source =>
    {
      source.Read(read =>
      {
        read.Action("GetClass", "Home");
      })                       
    })
    .OptionLabel(" -- Select --")                  
 )
}

Regards,
Dimitar
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
DropDownList
Asked by
Graham
Top achievements
Rank 2
Iron
Iron
Answers by
Dimitar
Telerik team
Share this question
or