2014-09-10 58 views
1

我在HTML表格中顯示SQL Server視圖vw_FormattedData。我正嘗試使用基於用戶輸入的值更新列superUserLevel。 (一個HTML選擇輸入)以SelectList默認值顯示SQL查詢爲HTML表格

基本上我試圖設置選擇的HTML選擇框(SelectList)的值,無論是在我的視圖。

有沒有在我的Index.chtml循環中傳遞@ item.superUserLevel到這個方法?我可能會以完全錯誤的方式解決這個問題。任何幫助深表感謝。

enter image description here

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

您可以使用Ajax發送回新值,嘗試與[knockoutjs](http://knockoutjs.com/),另請參閱[knockoutmvc](http://knockoutmvc.com/) – Max 2014-09-10 10:48:35

+0

我沒有問題發送回值。它試圖用已經存在於視圖中的值填充下拉框。 – 2014-09-10 13:19:00

+0

何時以及如何知道選定的值?然而,如果你知道客戶端的價值,你可以使用jQuery來選擇下拉的值 – Max 2014-09-10 13:36:16

回答

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定義的選項,並會結合的vw_FormattedData.superUserLevel值(你沒有包含你的模型定義,所以我只是嘲笑g該物業被命名爲superUserLevel)。因此,如果您有3個選項的值爲1,2和3,並且superUserLevel的值爲2,則默認情況下會選擇第二個選項。

編輯

原來DropDownListFor()不循環正常工作。解決方法是使用

@Html.DropDownListFor(m => m.Data[i].superUserLevel, new SelectList(Model.Levels, "id", "levelDescription", Model.Data[i].superUserLevel)) 

和視圖模型屬性需要改爲

public IEnumerable<Level> Levels { get; set; } 

,並在控制器

var queryLevels = from a in db.Level select a; 
model.Levels = queryLevels; 
+0

我得到@ Html.DropDownListFor(Model.Data [i] .superUserLevel,Model.Levels編譯錯誤 類型參數方法'System.Web.Mvc.Html.SelectExtensions.DropDownListFor (System.Web.Mvc.HtmlHelper ,System.Linq.Expressions.Expression >,System.Collections .Generic.IEnumerable )'不能從使用中推斷出來。嘗試明確指定類型參數。 – 2014-09-11 09:43:39

+0

是'int'的'superUserLevel'類型嗎?還是它是一個複雜的屬性? – 2014-09-11 09:44:51

+0

public int? superUserLevel {get;組; } – 2014-09-11 09:48:26