关于如何设置下拉列表MVC的选定值的问题 [英] Problem on how to set selected value of a dropdownlist MVC

查看:117
本文介绍了关于如何设置下拉列表MVC的选定值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我对如何设置下拉列表MVC的选定值有疑问.下面是我的代码:Controller

Hi,
I have a Problem on how to set selected value of a dropdownlist MVC. Below is my code: Controller

List<SelectListItem> mlist = new List<SelectListItem>();
          mlist.Add(new SelectListItem { Text = "-Please select Option-", Value = "Select" });
          mlist.Add(new SelectListItem { Text = "No", Value = "No" });
          mlist.Add(new SelectListItem { Text = "Yes", Value = "Yes" });
        ViewData["myList"] = mlist;



我的问题是如何在View上设置/预定义所选值.下面是查看代码:



My question is How can I set/predefine selected value on View. Below is view code:

foreach (var i in Model)
              {
      Is Member valid:@Html.DropDownList("mList", (IEnumerable<SelectListItem>)ViewData["myList"])
              }


在帖子上:


On Post:

 [HttpPost]
        public ActionResult GetMyList(string mList)
        {
....

return View();
}


谢谢!

推荐答案

hi
您要在下拉列表中将保存值显示为选定值吗?
您是否在寻找相同的商品?


这里的示例代码可能会帮助您:-
在视图中:-
<%= Html.DropDownListFor(model => model.DropDownId,(IEnumerable< SelectListItem>)MVC.Models.GenericModel.GetDropDownList(),>> Select")%>


在模型中:-

公共静态SelectList GetDropDownList()
{
List< dropdownalllistvalue> ddlList =在此获取所有dropdownlist值;
返回新的SelectList(ddlList,"ID","DropDownValue");
}

谢谢,
Ashish
hi
you want to show the save value in dropdownlist as selected value?
Are you looking for same ?


Here sample code may it will help you:--
In View:-
<%= Html.DropDownListFor(model => model.DropDownId, (IEnumerable<SelectListItem>)MVC.Models.GenericModel.GetDropDownList(), ">> Select")%>


In Model:-

public static SelectList GetDropDownList()
{
List<dropdownalllistvalue> ddlList = get all dropdownlist value here;
return new SelectList(ddlList ,"ID", "DropDownValue");
}

Thanks,
Ashish


你好!

您应该在控制器中使用其属性 Selected 标记要选择的一个选项.
所以看起来像这样:

Hi there!

You should mark one option that you want to be selected with its property Selected in your controller.
So it will be look like this:

List<SelectListItem> mlist = new List<SelectListItem>();
           mlist.Add(new SelectListItem { Text = "-Please select Option-", Value = "Select" });
           mlist.Add(new SelectListItem { Text = "No", Value = "No", Selected = true});
           mlist.Add(new SelectListItem { Text = "Yes", Value = "Yes" });
         ViewData["myList"] = mlist;


这篇关于关于如何设置下拉列表MVC的选定值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆