我的问题:dropdownlistfor在加载页面时没有选择正确的字符串值。以下是代码和视图:
Surcharges.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”,未通过下拉列表选择。
由于
答案 0 :(得分:0)
我的附加费表和数据如下所示:
对于正确运作并与桌子绑定的模型,您有什么建议?
使用带有编辑和删除按钮而不是foreach表的网格是否更好?
我正在寻找最佳模型,它实际上允许编辑每一行并保存到表格中(类型字段的下拉列表)。