简体   繁体   English

在MVC中单击“提交”按钮后,如何保持下拉列表中的选定值?

[英]How to keep selected value in dropdownlist selected after click submit button in MVC?

In my View I have this dropdownlist that returns data from database: 在我的视图中,我有一个下拉列表,该列表从数据库返回数据:

@Html.DropDownList("somevalue", (SelectList) ViewBag.DropList, "--Select Customer--", Model.Search.SomeValue)

But after I click search button: 但是,我单击搜索按钮后:

<input class="first-search_btn" type="submit" value="Search"/>

Operation is executed, but value from dropdown list returns to first empty value "Select Customer". 操作已执行,但下拉列表中的值返回到第一个空值“ Select Customer”。 How can I keep selected value from dropdownlist after I click "Search". 单击“搜索”后,如何保留下拉列表中的选定值。

Try to use use DropDownListFor like I did: 像我一样尝试使用DropDownListFor:

@Html.DropDownListFor(m => m.SubscriptionTypeId, new SelectList(ViewBag.SubscriptionTypes, "Key", "Value", Model.SubscriptionTypeId), new{ @class = "w100" })

where Model.SubscriptionTypeId is default selected value and 其中Model.SubscriptionTypeId是默认选择的值,而

ViewBag.SubscriptionTypes = Enum.GetValues(typeof(SubscriptionTypeDefinition)).Cast<SubscriptionTypeDefinition>().ToDictionary(item => (int)item, item => item.ToLocalizedString());

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

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