[英]how to set the selected value in the drop down list populated with ViewData in mvc
我是MVC的新手,請幫助我在使用“查看數據”填充的下拉列表中設置所選值。 我經歷了很多解決方案,但是每個解決方案都涉及為單個下拉列表選擇值。 我有相同的下拉列表使用foreach
循環。 為該foreach
循環中的每個下拉菜單設置選定的值。 我的代碼如下所示。
[查看中]
int i = 0;
foreach (var item in Model.Select((x, j) => new { Data = x, Index = j + 1 })) {
@Html.DropDownListFor(model => item.Data.CategoryID,(IEnumerable<SelectListItem>)ViewData["categories"], new { @id = "category" + i })
i++;
}
[在控制器中]
SelectList selectList = new SelectList((IEnumerable<Category>)ConvertCategoryToList(dt1), "CategoryID", "CategoryName");
ViewData["categories"] = selectList;
您的示例中缺少很多內容(例如,類別是什么樣的,Edit操作是什么樣, dt1
是什么,您傳遞給視圖的模型是什么,等等),但我將根據顯示的內容進行嘗試。
您不能將foreach
與一組表單項一起使用。 這些項目無法知道要綁定哪個元素(將它們發送回服務器時,它們需要更多信息以使每個條目唯一)。
解:
它僅在使用索引時才知道如何綁定(例如,使用for (int i=0; < count; i++){ @Html.DropDownListFor(model=>item[i]...
)
這是DropDownListFor
最令人討厭的功能,似乎是一個錯誤。 它不會從綁定數據中獲取當前選擇的值。 它只會從SelectList中獲取當前值,這意味着每個下拉列表都需要一個唯一的SelectList 。
解:
在所需的每個下拉列表中,在視圖中創建一個唯一的SelectList
,並將項目值作為當前選擇。
通過偽造您的數據,我可以進行以下工作:
僅將SelectList的項目列表存儲在ViewBag中:
ViewData["categories"] = ConvertCategoryToList(dt1);
1.您需要傳遞一個List
而不是IEnumerable
因為IEnumerable無法索引。
@model List<MyApplication.Item>
2.為每個下拉列表創建一個SelectList
SelectList newSelectList = new SelectList((IEnumerable<MyApplication.Category>)ViewData["categories"], "CategoryID", "CategoryName", Model[i].Id);
3.使用索引方法處理集合中的項目。
@Html.DropDownListFor(m => Model[i].Id, newSelectList, "Select an item")
就目前而言,您的代碼的另一個問題是在編輯視圖之間來回傳遞的內容是什么? 目前,我會猜測(從變量名dt1開始 )您正在傳遞DataTable
(?)。 您將需要非常清楚地了解正在使用的數據,以便對此問題有一個清晰的解決方案。 我建議您發布所有代碼和Razor查看HTML的第二個問題。
如果您需要查看更多我的示例代碼(如下),請發布您自己的代碼,以便使它們一致。
public class TestController : Controller
{
public List<Category> dt1 { get; set; }
public TestController()
{
this.dt1 = new List<Category>();
for (int i = 1; i <= 10; i++)
{
this.dt1.Add(new Category() { CategoryId = i, CategoryName = string.Format("Category {0}", i) });
}
}
[HttpGet]
public ActionResult Edit()
{
//SelectList selectList = new SelectList((IEnumerable<Category>)ConvertCategoryToList(dt1), "CategoryID", "CategoryName");
ViewData["categories"] = ConvertCategoryToList(dt1);
List<Item> items = new List<Item>();
items.Add(new Item(){Id = 1});
items.Add(new Item(){Id = 2});
items.Add(new Item(){Id = 3});
items.Add(new Item(){Id = 4});
items.Add(new Item(){Id = 5});
return View(items);
}
[HttpPost]
public ActionResult Edit(FormCollection form)
{
// Turn form submission back into a compatible view model
List<Item> items = new List<Item>();
foreach (string key in form.Keys)
{
string val = form[key];
items.Add(new Item() { Id = int.Parse(val) });
}
// Recreate the select list
ViewData["categories"] = ConvertCategoryToList(dt1);
return View(items);
}
List<Category> ConvertCategoryToList(IEnumerable<Category> dt)
{
return dt.ToList();
}
}
注意:編輯的發布版本只是重新創建數據列表(使用回傳的選定值)並返回到編輯視圖。 這僅用於測試。
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}
@model List<MyApplication.Item>
@using (Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
SelectList newSelectList = new SelectList((IEnumerable<MyApplication.Category>)ViewData["categories"], "CategoryID", "CategoryName", Model[i].Id);
@Html.DropDownListFor(m => Model[i].Id, newSelectList, "Select an item")
}
<input type="submit" value="Submit"/>
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.