我在HTML表格中显示SQL Server视图 vw_FormattedData 。我正在尝试使用基于用户输入的值更新列 superUserLevel 。 (HTML选择输入)
基本上我正在尝试将HTML选择框(SelectList)的选定值设置为我视图中的任何内容。
我的Index.chtml循环中有没有办法将@ item.superUserLevel传递给它?我可能会以完全错误的方式解决这个问题。非常感谢任何帮助。
HomeController.cs
namespace FormPostTest.Controllers
{
public class HomeController : Controller
{
private DannyContext db = new DannyContext();
public ActionResult Index()
{
ViewData["OurData"] = db.vw_FormattedData.ToList();
var queryLevels = from a in db.Level select a;
ViewData["Levels"] = new SelectList(queryLevels, "id", "levelDescription", "DONT KNOW WHICH IS SELECTED FROM HERE");
return View();
}
}
}
Index.chtml
@model FormPostTest.Models.vw_FormattedData
@{
ViewBag.Title = "Home Page";
}
<div class="row top-buffer">
<div class="col-md-12">
<table class="table table-striped top-buffer">
<tr>
<th>
Forename
</th>
<th>
Surname
</th>
<th>
Line1
</th>
<th>
Line2
</th>
<th>
City
</th>
<th>
Country
</th>
<th>
Super User Level
</th>
</tr>
@foreach (var item in @ViewData["OurData"] as List<FormPostTest.Models.vw_FormattedData>)
{
<tr>
<td>
@item.forename
</td>
<td>
@item.surname
</td>
<td>
@item.line1
</td>
<td>
@item.line2
</td>
<td>
@item.city
</td>
<td>
@item.country
</td>
<td>
@Html.DropDownList("Levels", null, "** Please Select **", new { @class = "form-control" })
</td>
</tr>
}
</table>
</div>
</div>
SQL
create table person(
id int not null identity(1,1) primary key,
forename varchar(50) not null,
surname varchar(50) not null
);
insert into person (forename, surname) values ('Arsene', 'Wenger');
insert into person (forename, surname) values ('Nigel', 'Pearson');
create table address(
id int not null identity(1, 1) primary key,
line1 varchar(50),
line2 varchar(50),
city varchar(50),
country varchar(50),
personId int not null
);
insert into address (line1, line2, city, country, personId) values ('Emirates Stadium', '75 Drayton Park', 'London', 'England', 1);
insert into address (line1, line2, city, country, personId) values ('King Power Stadium', 'Filbert Way', 'Leicester', 'England', 2);
create table superuser(
id int not null identity(1, 1) primary key,
personId int not null,
permissionLevel int
);
create view vw_FormattedData as
select
p.forename,
p.surname,
a.line1,
a.line2,
a.city,
a.country,
su.permissionLevel as superUserLevel
from
person p
left join
address a
on a.personId = p.id
left join
superuser su
on su.personId = p.id;
create table levels(
id int not null primary key,
levelDescription varchar(50)
);
insert into levels (id, levelDescription) values (1, 'One');
insert into levels (id, levelDescription) values (2, 'Two');
insert into levels (id, levelDescription) values (3, 'Three');
答案 0 :(得分:2)
您没有正确使用DropDownList
,但无论如何都不会正确回发(所有下拉列表都具有相同的ID和名称属性)。
查看模型
public class MyViewModel
{
public IList<vw_FormattedData> Data { get; set; }
public SelectList Levels { get; set; }
}
控制器
public ActionResult Index()
{
MyViewModel model = new MyViewModel();
model.Data = db.vw_FormattedData.ToList();
var queryLevels = from a in db.Level select a;
model.Levels = new SelectList(queryLevels, "id", "levelDescription");
return View(model);
}
查看
@model YourAssembly.MyViewModel
....
@for(int i = 0; i < Model.Data.Count; i++)
{
<tr>
<td>@Model.Data[i].forename</td>
....
<td>@Html.DropDownListFor(m => m.Data[i].superUserLevel, Model.Levels)</td>
</tr>
这将使用SelectList Levels
定义的选项创建一个select,并将绑定到vw_FormattedData.superUserLevel
的值(您没有包含模型定义,所以我只假设该属性名为{{ 1}})。因此,如果您有3个值为1,2和3的选项,并且superUserLevel
的值为2,则默认情况下将选择第二个选项。
修改强>
结果superUserLevel
在循环中无法正常工作。解决方法是使用
DropDownListFor()
并且视图模型属性需要更改为
@Html.DropDownListFor(m => m.Data[i].superUserLevel, new SelectList(Model.Levels, "id", "levelDescription", Model.Data[i].superUserLevel))
并在控制器中
public IEnumerable<Level> Levels { get; set; }