[英]How to bind an ID to the selected value of an @Html.DropDownList within a WebGrid
I have a View in my MVC project that has a WebGrid which is binded by an "Account" model that I passed into the View. 我在我的MVC项目中有一个View,它有一个WebGrid,它被我传入View的“Account”模型绑定。
In my "Account" Controller I created a List of SelectedListItem's that I contains the DropDownList options which I then set to a ViewBag: 在我的“帐户”控制器中,我创建了一个SelectedListItem列表,其中包含DropDownList选项,然后我将其设置为ViewBag:
public ActionResult Index()
{
var accounts = db.Accounts;
var groups = db.Groups;
List<SelectListItem> groupList = new List<SelectListItem>();
foreach(var item in groups)
{
groupList.Add(new SelectListItem()
{
Value = item.group_id.ToString(),
Text = item.group_name
});
}
ViewBag.Groups = groupList;
return View(accounts);
}
The DropDownList contains 3 entries with their value and text as follows: DropDownList包含3个条目及其值和文本,如下所示:
1, One 2, Two 3, Three 1,一,二,三,三
My problem is getting the group_id (Value) of the binded data to properly display the group_name (Text) on the DropDownList. 我的问题是获取绑定数据的group_id(Value)以正确显示DropDownList上的group_name(Text)。
This is what I have so far: 这是我到目前为止:
grid.Column("group_id","Group", format: (item) => @Html.DropDownList("GroupId", (List<SelectListItem>)ViewBag.Groups))
The DropDownList does contains all 3 values I mentioned previously, it just doesn't set the DropDownList to the proper one at all for all the binded Accounts, as the image shows: DropDownList确实包含我之前提到的所有3个值,它只是没有将DropDownList设置为所有绑定帐户的正确值,如图所示:
I have edited this post to add my View code. 我已编辑此帖子以添加我的查看代码。
@model IEnumerable<Account>
@{
ViewBag.Title = "Index";
WebGrid grid = new WebGrid(Model, rowsPerPage: 10);
}
<h2>Fee Invoices</h2>
@grid.GetHtml(tableStyle: "table table-bordered",
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: grid.Columns(
grid.Column("account_name", "Account"),
grid.Column("account_number", "Account Number"),
grid.Column("as_of_date", "Date", format: (item) => string.Format("{0:MM/dd/yyyy}", item.as_of_date)),
grid.Column("approved", "Approved", format: @<text><input id="select" class="box" name="select" type="checkbox" @(item.approved ? "checked='checked'" : "") value="@item.approved" /></text>),
grid.Column("group_id","Group", format: (item) => @Html.DropDownList("GroupId", (List<SelectListItem>)ViewBag.Groups))
)
))
You can pass a Dictionary<int, string>
of group_id
and group_name
to your view instead of the list of SelectListItem
and then use it to create the DropDownList
with the correct value selected. 您可以将
group_id
和group_name
的Dictionary<int, string>
group_id
给视图而不是SelectListItem
列表,然后使用它创建DropDownList
并选择正确的值。
In the controller 在控制器中
public ActionResult Index()
{
var accounts = db.Accounts;
var groups = db.Groups;
// this line creates a Dictionary<int, string> where group_id is the key and group_name the value
var groupsNames = groups.ToDictionary(x => x.group_id, x => x.group_name);
ViewBag.GroupsNames = groupsNames;
return View(accounts);
}
Then in the view declare a function like this (usually before the html part) 然后在视图中声明一个这样的函数(通常在html部分之前)
@functions
{
public List<SelectListItem> CreateSelectList(int groupId)
{
var newList = new List<SelectListItem>();
foreach (var val in (Dictionary<int, string>)ViewBag.GroupsNames)
{
newList.Add(new SelectListItem
{
Text = val.Value,
Value = val.Key.ToString(),
Selected = val.Key == groupId
});
}
return newList;
}
}
and use it to populate the drop down list 并使用它来填充下拉列表
grid.Column("group_id", "Group", format: (item) => Html.DropDownList("GroupId", CreateSelectList((int)item.group_id)))
Or, if you don't need the drop down list but instead just want to display the name of the group you can do 或者,如果您不需要下拉列表,而只想显示您可以执行的组的名称
grid.Column("group_id", "Group", format: (item) => ((Dictionary<int, string>)ViewBag.GroupsNames)[item.group_id])
and in this case you don't need the function. 在这种情况下,您不需要该功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.