简体   繁体   English

如何从下拉菜单中选择默认值

[英]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? 如果当前值为空,如何将下拉选择的值设置为已经为“ Rate1”? (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? 最后,当模态加载选定的值时,将@class =“ form-control”的状态设置为:@class =“ form-control edited”?

I have this as a lookup fill with 4 rates in my drop down: 我将其作为查找填充,下拉菜单中包含4个比率:

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查找下拉列表的BillingRates:

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: 然后在我的cshtml文件中有此变量:

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: 最后,我将其作为我的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; 我最终只是简单地从上面替换了它: var selectedBillingRate = string.Empty; with this: var selectedBillingRate = "Rate1"; 这样做: 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. 我意识到这可能不是最佳方法,而@StephenMueke的方法则是最佳方法,但是,我找不到合适的GET方法来预先设置BillingRate的值。 My bad, still learning. 我不好,还在学习。 Thanks again @StephenMuecke for commenting and leading me to research it further. 再次感谢@StephenMuecke发表评论并带领我进行进一步研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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