我在HTML表格中顯示SQL Server視圖vw_FormattedData。我正嘗試使用基於用戶輸入的值更新列superUserLevel。 (一個HTML選擇輸入)以SelectList默認值顯示SQL查詢爲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');
您可以使用Ajax發送回新值,嘗試與[knockoutjs](http://knockoutjs.com/),另請參閱[knockoutmvc](http://knockoutmvc.com/) – Max 2014-09-10 10:48:35
我沒有問題發送回值。它試圖用已經存在於視圖中的值填充下拉框。 – 2014-09-10 13:19:00
何時以及如何知道選定的值?然而,如果你知道客戶端的價值,你可以使用jQuery來選擇下拉的值 – Max 2014-09-10 13:36:16