如何将ID绑定到WebGrid中@ Html.DropDownList的选定值

问题描述 投票:0回答:1

我在我的MVC项目中有一个View,它有一个WebGrid,它被我传入View的“Account”模型绑定。

在我的“帐户”控制器中,我创建了一个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);
}

DropDownList包含3个条目及其值和文本,如下所示:

1,一,二,三,三

我的问题是获取绑定数据的group_id(Value)以正确显示DropDownList上的group_name(Text)。

这是我到目前为止:

grid.Column("group_id","Group", format: (item) => @Html.DropDownList("GroupId", (List<SelectListItem>)ViewBag.Groups))

DropDownList确实包含我之前提到的所有3个值,它只是没有将DropDownList设置为所有绑定帐户的正确值,如图所示:

Account WebGrid

我已编辑此帖子以添加我的查看代码。

@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))
)


))
c# asp.net-mvc html-select webgrid
1个回答
0
投票

您可以将Dictionary<int, string>group_idgroup_name传递给您的视图而不是SelectListItem列表,然后使用它来创建选择了正确值的DropDownList

在控制器中

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);
}

然后在视图中声明一个这样的函数(通常在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;
    }
}

并使用它来填充下拉列表

grid.Column("group_id", "Group", format: (item) => Html.DropDownList("GroupId", CreateSelectList((int)item.group_id)))

或者,如果您不需要下拉列表,而只想显示您可以执行的组的名称

grid.Column("group_id", "Group", format: (item) => ((Dictionary<int, string>)ViewBag.GroupsNames)[item.group_id])

在这种情况下,您不需要该功能。

© www.soinside.com 2019 - 2024. All rights reserved.