簡體   English   中英

填寫MVC下拉列表的值

[英]Fill values for mvc dropdownlist

 @Html.DropDownListFor(x => x.Pets, new SelectList(ViewBag.Pets, "key", "value"), new { @class = "type1 newdrop" })

我是MVC開發的新手。 使用上面的代碼行,我得到了下拉列表,但只有2作為值。

如何將下拉列表填充到所有小於2值(包括0 我希望所選項目為2

 ViewBag.Pets = Constants.MaxP.Where( x => x.Key == someId); // the someId is for sure one of the below key numbers

哪里

  public static Dictionary<int, int>   MaxP = new Dictionary<int, int>(){
                {12, 2}, {13, 2}, {14, 2}, {15, 2}, {16, 2}, {17, 2}, {18, 2}
            };

將字典變成SelectListItem的集合,如下所示:

int a = Constants.Maxp.FirstOrDefualt(x => x.Key == SomeID).Value;

List<SelectListItem> lst = new List<SelectListItem>();

for (int i = 0; i < a; i++)
{
    lst.Add(new SelectListItem(){
        Text = i.ToString(),
        Value = i.ToString()
    });
}

ViewBag.Pets = lst;

然后在您的視圖中:

@Html.DropDownListFor(x => x.Pets, ViewBag.Pets, new { @class = "type1 newdrop" })

其中x.pets是與Key匹配的ID

var certainKey = 15;
var certainValue = 2;

用所有key less than certainKey項填充dropdownlist

 ViewBag.Pets = Constants.MaxP.Where( x => x.Key < certainKey);

用所有value less than certainValue項目填充dropdownlist

 ViewBag.Pets = Constants.MaxP.Where( x => x.Value < certainValue);

用具有與key equal with certainKeyvalue less than certainValue的項填充dropdownlist

 ViewBag.Pets = Constants.MaxP.Where( x => x.Key == certainKey && x.Value < certainValue);

對於邏輯和功能:

  • Dictionary Key (id)應該是唯一的,並且Value可以是重復的

  • SelectList Value (id)應該唯一,Text可以重復

  • 最后,在dropdownlist列表中使用SelectList然后在將dictionary用於dropdownlist ,應在Dicatinary基礎上創建SelectList 您正在執行此操作,轉換模式如下所示:
  • Dicatinary >>>> SelectList
  • Dictionary >> SelectList 文本

現在確定要在字典選擇中使用的條件,以達到目標。


編輯:

創建SelectlistDictinary並設置為默認選擇的值作為Max value

Dictionary<int,int> myDictionary = GetDictionary();
var pets=new SelectList(myDictionary, "key","value", myDictionary.Max(x => x.Key));

通過填充的selectList進行查看

ViewBag.Pets = pets;

在視圖中:

@{
  var myPets = ViewBag.Pets as Dictionary<int,int>;
}

@Html.DropDownListFor(x => x.Pets, myPets, new { @class = "type1 newdrop" })

現在,您只需要根據應用程序邏輯編寫GetDictionary()方法代碼即可創建適當的數據字典。

這是我們通常填充下拉列表的方式。

例如,您為要在下拉菜單中填充的商品創建一個類:

public class MaxP
{
    public int Key { get; set; }
    public int Value { get; set; }
}

然后,在控制器中執行操作時,您將如下所示填充選擇列表:

var list = new List<MaxP>();
list.Add(new MaxP { Key = 12, Value = 2 });
list.Add(new MaxP { Key = 13, Value = 2 });
list.Add(new MaxP { Key = 14, Value = 2 });

ViewBag.Pets = new SelectList(list, "Key", "Value", null);

在您看來:

@Html.DropDownListFor(x => x.Pets, (SelectList)ViewBag.Pets, new { @class = "type1 newdrop" });

請注意,將在下拉列表中顯示的文本為“ 2”。 如果選擇了該值,您將獲得的值是“鍵”,在我們的示例中為12、13和14。

希望清楚。 從初學者的角度來看,它可能很復雜,但是沒關系,我們所有人都去過那里。 但我鼓勵您開始做基本的事,做好自己的事。 請從基礎開始學習。 這是一個很好的起點: http : //www.asp.net/mvc/overview/older-versions-1/getting-started-with-mvc/getting-started-with-mvc-part1

代碼未經測試,但希望您明白了,學習愉快。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM