简体   繁体   English

在ASP.Net MVC中回发后,在页面重新加载时保持下拉菜单处于选中状态

[英]Keep dropdown option selected on page reload after postback in ASP.Net MVC

As i know in DropdownListFor or DropdownList in MVC there is no option for keep option selected after postback or after Edit the record. 如我所知,在MVC的DropdownListFor或DropdownList中,没有回传或编辑记录后保持选项被选中的选项。

Can someone tell how to achieve this? 有人可以告诉我如何实现吗?

here is my sample code 这是我的示例代码

@using (Html.BeginForm())
{
    @Html.DropDownList("menuitems",Model._menu,"Select Menu")
}

here _menu is IEnumerable type list. _menu是IEnumerable类型列表。 While this page loads on browser, i want particular DropDownList Option selected by index number or some other way 在浏览器上加载此页面时,我希望通过索引号或其他方式选择特定的DropDownList选项

List Model - 列表模型-

public class MenuListModel
    {
        public long menuid { get; set; }
        public string menuname { get; set; }
    }

Getting List from Database 从数据库获取列表

public IEnumerable<MenuListModel> GetMenuItems()
        {
            List<MenuListModel> _MenuListModel = new List<MenuListModel>();
            var query = (from q in db.menus.Where(c => c.valid == true) select new{menuid=q.menuid,menuname=q.menuname});
            foreach (var row in query.ToList())
            {
                MenuListModel _menu = new MenuListModel();
                _menu.menuid = row.menuid;
                _menu.menuname = row.menuname;
                _MenuListModel.Add(_menu);
            }
            return _MenuListModel;
        }

Controller 控制者

IEnumerable<MenuListModel> _MenuListModel = ftwCommonMethods.GetMenuItems();
UserRightViewSearch _UserRightViewSearch = new UserRightViewSearch();
_UserRightViewSearch._menu = _MenuListModel;
return View(_UserRightViewSearch);

Thanks in advance. 提前致谢。

Take a simple case to demo, where user selected dropdown and submit full page and some validation failed in controller or some other reason you want to send to same screen and retain selected value... In that case try with following modifications as an example and then you can extend to your requirement.. 以一个简单的案例进行演示,在该案例中,用户选择了下拉列表并提交了整页,并且控制器中的某些验证失败,或者您想发送至同一屏幕并保留所选值的其他原因。在这种情况下,请尝试以下修改作为示例,那么您可以扩展您的要求。

View Code: 查看代码:

@Html.DropDownList("SelectedMenuItem", Model._menu.Select(menu => new SelectListItem { Text = menu.menuname, Value = menu.menuid.ToString() }), "--Select Menu--")

Main View Model: 主视图模型:

public class UserRightViewSearch
{
   public IEnumerable<MenuListModel> _menu { get; set; }
   public long SelectedMenuItem { get; set; } //Property to hold dropdown selection
}

Controller Action: 控制器动作:

[HttpPost]
public ActionResult Index(UserRightViewSearch userRightView)
{
      //Here may be validation failed or for some other reason, return to same view
      userRightView._menu = GetMenuItems(); //Just for demo, but somehow you need to populate the default data to show as listbox items here, otherwise you see null reference exception or no items in list based on how you handle the case
      return View(userRightView);
}

Hope this provide you some idea to explore further.. 希望这为您提供一些进一步探索的想法。

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

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