简体   繁体   中英

How to Select a default value from dropdown

This works now requiring the user to select one of the drop-down values. How do I set the drop-down selected Value to already be "Rate1", if the current value is empty? (which is how it is when the Modal loads)

and finally, set the state of @class = "form-control" to: @class = "form-control edited" when the modal loads with the selected Value?

I have this as a lookup fill with 4 rates in my drop down:

case "BillingRates":
            {
                strArrays = new string[] { "Rate1", "Rate2", "Rate3", "Rate4" };
                for (i = 0; i < (int)strArrays.Length; i++)
                {
                    string str4 = strArrays[i];
                    lookups.Add(new Lookup()
                    {
                        Text = str4,
                        Value = str4,
                        TenantId = currentTenantId,
                        Selected = false,
                        Disabled = false,
                        Group = null,
                        AssociatedClass = string.Empty
                    });
                }
                break;
            }

I also use this foreach to lookup the BillingRates for the dropdown:

foreach (Lookup lookupItem in (new LookupFill("BillingRates", num)).LookupItems)
        {
            SelectListItem selectListItem3 = new SelectListItem()
            {
                Text = lookupItem.Text,
                Value = lookupItem.Value,
                Disabled = lookupItem.Disabled,
                Selected = lookupItem.Selected
            };
            selectListItems5.Add(selectListItem3);
        }
        SelectListItem selectListItem4 = new SelectListItem()
        {
            Text = "",
            Value = "",
            Disabled = false
        };

Then I have this in my cshtml file for variables:

var selectedBillingRate = string.Empty;
var BillingRates = ViewData["BillingRates"] as IEnumerable<SelectListItem>;
foreach (var item in BillingRates)
{
    if (item.Value == Model.Invoice.BillingRate) { selectedBillingRate = item.Value; break; }
}
var BillingRateSelect = new SelectList(BillingRates, "Value", "Text");


var selectedBillingRate = '@Html.Raw(selectedBillingRate)';
        $('#BillingRate').val(selectedBillingRate);

Finally, I have this as my DropDownList:

@Html.DropDownList("BillingRate", BillingRateSelect, new { id = "BillingRate", required = "required", @class = "form-control" + (!string.IsNullOrEmpty(Model.Invoice.BillingRate) || !string.IsNullOrEmpty(selectedBillingRate) ? " edited" : "") })

I ended up simply replacing this from above: var selectedBillingRate = string.Empty; with this: var selectedBillingRate = "Rate1"; and it seems to work as requested.

I realize this is probably not the best way to achieve and @StephenMueke's way is the best way to do this, however, I could not find the right GET method to set the value of the BillingRate as desired beforehand. My bad, still learning. Thanks again @StephenMuecke for commenting and leading me to research it further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM