【问题标题】:MVC Html.DropDownListFor for simple list of strings, Model: IEnumerable<IEnumerable<string>>MVC Html.DropDownListFor 用于简单的字符串列表,模型:IEnumerable<IEnumerable<string>>
【发布时间】:2018-03-02 16:26:10
【问题描述】:

我的问题:dropdownlistfor 在加载页面时没有选择正确的字符串值。这是代码和视图:

附加费.html

@model IEnumerable<IEnumerable<string>>
@{
   ViewBag.Title = "Surcharge Management";
}
<div id="mainTitleDiv" class="mainTitle">
    <span style="display:inline-block;">@ViewBag.Title</span>
</div>
<br />
<table class="table">  
@{
    int i = 0;
    foreach (var surcharge in Model)
    {
        int j = 0;
        <tr>
            @foreach (var item in surcharge)
            {
                if (i == 0)
                {
                    @:<th>@Html.DisplayFor(model => item)</th>
                }
                else
                {
                    if (j == 0)
                    {
                        @:<th>@Html.DisplayFor(model => item)</th>
                    }
                    else
                    {
                        @:<td>
                        if (!string.IsNullOrEmpty(item) && (i == 2)) // Type
                        {
                            @Html.DropDownListFor(x => item, new List<SelectListItem>
                            {
                                new SelectListItem() {Text = "Fixed Amount", Value ="0"},
                                new SelectListItem() {Text = "Percentage",  Value ="1"}
                            }, new { style = "width:100%; height:34px;" } )
                        }
                        else
                        {
                            @Html.EditorFor(model => item, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => item, "", new { @class = "text-danger" })
                        }
                        @:</td>
                    }
                    j++;
                }
            }
        </tr>
        i++;
    }
}

AdminController.cs

public ActionResult Surcharges()
{
    if (!User.Identity.IsAuthenticated)
    {
        return RedirectToAction("Login", "Account");
    }

    int userId = User.Identity.GetUserId<int>();
    var query = db.Surcharges.Where(x => x.UserId == userId).OrderBy(x => x.Id).ToList();
    IEnumerable<IEnumerable<string>> surcharges = Utility.Pivot<string>(Utility.GetSurchargeList(query));            

    return View(surcharges);
}

附加费页面 Surcharge

S2 和 D1 的字符串值为“1”,并且未被下拉列表选中。

  1. 我做错了什么?
  2. 我需要一个模型来将编辑后的数据发送回服务器吗?
  3. 正确的模型应该是什么样的?

谢谢

【问题讨论】:

标签: asp.net-mvc model ienumerable html.dropdownlistfor


【解决方案1】:

谢谢,斯蒂芬。我的 ASP.NET MVC 项目要求是设计一个如下图所示的页面:

Surcharge Page

我的附加费表和数据如下所示:

Surcharge Table

您对能够正常工作并绑定到表格的模型有什么建议?

使用带有编辑和删除按钮的网格而不是 foreach 表更好吗?

我正在寻找实际上允许编辑每一行并将其保存到表中的最佳模型(类型字段的下拉列表)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多