本文介绍了如何根据文本框中显示的值为C#MVC在下拉列表中自动选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi All C# MVC programmers
Could you please help me
How do I auto select selected Item value in dropdownlistfor(),
base on the value displayed by @html.TextboxFor()
I display values in dropdownlist implementing listC
listC
public void ListCDropDownList()
{
var itemsQuery = from k in db.listCs select k;
ViewBag.ItemsList = itemsQuery.ToList();
}
+-------+---------+---------+
| id | Code | ItemName|
+-------+---------+---------+
| 1 | 3015AZ | sugar |
+-------+---------+---------+ this is listC
| 2 | 301502 | salt |
+-------+---------+---------+
| 3 | 509801 | tuna |
+-------+---------+---------+
//TextboxFor displays for each item in table listA
+-------+---------+---------+
| id |TypeCode | MenuItem|
+-------+---------+---------+
| 1 |3015AZ-1 | Icecream|
+-------+---------+---------+ this is listA
| 2 |3015AZ-1 | Juice |
+-------+---------+---------+
| 3 |3015AZ-1 | Cake |
+-------+---------+---------+
<td>
@Html.TextBoxFor(modelItem => item.Code , new { @readonly = "readonly", @class = "form-control " })
</td>
<td>
@Html.TextBoxFor(modelItem => item.MenuItem, new { @readonly = "readonly", @class = "form-control" })
</td>
<td>
@Html.DropDownListFor("listC", new SelectList(ViewBag.ItemsList, "ItemName", "ItemName"), "Please Select", new { @class = "form-control" })
我尝试了什么:
What I have tried:
//HERE TODO
// HOW do I make an auto select value in Dropdowlist --
//When page loads, the dropdownlist will load the auto-select value base on the value displayed by Texboxfor
//sample: sugar; because this value is base on the value in texboxfor = Code
//Load listC in viewbag
public void ListCDropDownList()
{
var itemsQuery = from k in db.listCs select k;
ViewBag.ItemsList = itemsQuery.ToList();
}
//display listA
public ActionResult Index()
{
ListCDropDownList());
var menuList = db.listAs.ToList();
return View(menuList .OrderBy(x => x.MenuItem).ToList());
}
@model IEnumerable<App.Models.MYMOdel>
@foreach (var item in Model)
{
<td>
@Html.TextBoxFor(modelItem => item.Code , new { @readonly = "readonly", @class
= "form-control " })
</td>
<td>
@Html.TextBoxFor(modelItem => item.MenuItem, new { @readonly = "readonly", @class =
"form-control" })
</td>
<td>
@Html.DropDownListFor("listC", new SelectList(ViewBag.ItemsList, "ItemName",
"ItemName"), "Please Select", new { @class = "form-control" })
</td>
}
thank you everyone
推荐答案
public void ListCDropDownList(string code)
{
var itemsQuery = from k in db.listCs where k.code == code select k ;
ViewBag.ItemsList = itemsQuery.ToList();
}
public ActionResult Index(string code)
{
ListCDropDownList(code);
var menuList = db.listAs.ToList();
return View(menuList.OrderBy(x => x.MenuItem).ToList());
}
然后你可以修改你的 DropDownListFor
声明:
Then you can modify your DropDownListFor
declaration to this:
@Html.DropDownListFor(item => item.Code, new SelectList(ViewBag.ItemsList, "ItemName",
"ItemName"), "Please Select", new { @class = "form-control" })
这篇关于如何根据文本框中显示的值为C#MVC在下拉列表中自动选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!