Asp.net mvc MVC Html.DropDownListFor用于字符串的简单列表,型号:IEnumerable<;IEnumerable<;字符串>&燃气轮机;
我的问题:dropdownlistfor在加载页面时没有选择正确的字符串值。以下是代码和视图: 附加费.htmlAsp.net mvc MVC Html.DropDownListFor用于字符串的简单列表,型号:IEnumerable<;IEnumerable<;字符串>&燃气轮机;,asp.net-mvc,model,ienumerable,html.dropdownlistfor,Asp.net Mvc,Model,Ienumerable,Html.dropdownlistfor,我的问题:dropdownlistfor在加载页面时没有选择正确的字符串值。以下是代码和视图: 附加费.html @model IEnumerable<IEnumerable<string>> @{ ViewBag.Title = "Surcharge Management"; } <div id="mainTitleDiv" class="mainTitle"> <span style="display:inline-block;">
@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++;
}
}
@model IEnumerable
@{
ViewBag.Title=“附加费管理”;
}
@视图包。标题
@{
int i=0;
foreach(模型中的var附加费)
{
int j=0;
@foreach(附加费中的var项目)
{
如果(i==0)
{
@:@Html.DisplayFor(model=>item)
}
其他的
{
如果(j==0)
{
@:@Html.DisplayFor(model=>item)
}
其他的
{
@:
如果(!string.IsNullOrEmpty(item)&&(i==2))//类型
{
@DropDownListFor(x=>item,新列表
{
新建SelectListItem(){Text=“Fixed Amount”,Value=“0”},
新建SelectListItem(){Text=“Percentage”,Value=“1”}
},新{style=“宽度:100%;高度:34px;”})
}
其他的
{
@EditorFor(model=>item,new{htmlAttributes=new{@class=“formcontrol”})
@ValidationMessageFor(model=>item,“,new{@class=“text danger”})
}
@:
}
j++;
}
}
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);
}
公共行动结果附加费()
{
如果(!User.Identity.IsAuthenticated)
{
返回重定向操作(“登录”、“帐户”);
}
int userId=User.Identity.GetUserId();
var query=db.Surcharges.Where(x=>x.UserId==UserId).OrderBy(x=>x.Id).ToList();
IEnumerable surcharges=Utility.Pivot(Utility.GetSurchargeList(查询));
返回视图(附加费);
}
附加费页
S2和D1具有字符串值“1”,且未被dropdownlist选择
谢谢,斯蒂芬。我的ASP.NET MVC项目要求是设计一个类似以下图像的页面: 我的附加费表和数据如下所示: 关于正确工作并绑定到表的模型,您有什么建议 使用带有编辑和删除按钮的网格而不是foreach表是否更好
我正在寻找一种最好的模型,它实际上允许编辑每一行并将其保存到表中(类型字段为dropdownlist)。您不能使用
foreach
循环为集合生成表单控件-绑定不起作用(请参阅,您还需要参阅,以在循环中处理dropdownlist)