2015-01-13 63 views
0

我很困惑如何做到這一點,我看到這些:reference to similar question 1 & reference to similar question 2這兩個問題的答案都沒有解釋我如何綁定實際的數據表變量模型,因爲他們更多地指定到dropdownlist部分。如何將數據表綁定到模型,然後綁定到mvc中的下拉列表?

我嘗試過使用兩種方法:一種是將列表從模型綁定到數據表格字符串,另一種是使用通過在模型中創建字符串對象列表來綁定的字符串來創建模型類在控制器中。但是我的問題仍然在於它傳遞給它的數據變量和模型項在內部死亡。

那麼應該如何正確使用模型來綁定數據表到下拉列表呢?

控制器:

BFProj2.Models.OurColumns o = new Models.OurColumns(); 


       o.DCResults = new List<string>(); 

       List<OurColumns> s = new List<OurColumns>(); 


       for(int y = 0; y <csvData.Columns.Count; y++) 
       { 
        string dc = csvData.Columns[y].ColumnName.ToString(); 


        //When the list is created in the model. 
        o.DCResults.Add(dc.ToString()); 

        //when the list is created in the controller. 
        foreach(OurColumns dc1 in s) 
        { 
         dc1.result = dc; 


        } 

       } 

       //Still in try... 

       // 
       // 
       // Here the former binding to the database began. 
       // 
       // 


      } 
      catch (Exception ex) 
      { 
       //return View("Error"+ ex.GetType().ToString()); 
      } 
      //csvData is {Table1} 
     } 
     return View(); 
    } 

csvData是數據表。

型號:

namespace BFProj2.Models 
{ 
public class OurColumns 
{ 
    //[DisplayName("Password")] 
    public string Password { get; set; } 

    //[DisplayName("Email")] 
    public string Email { get; set; } 

    //[DisplayName("Comment")] 
    public string Comment { get; set; } 

    //[DisplayName("Username")] 
    public string UserName { get; set; } 

    //[DisplayName("Firstname")] 
    public string FirstName { get; set; } 

    //[DisplayName("Lastname")] 
    public string LastName { get; set; } 

    //[DisplayName("Last activity date")] 
    public DateTime? LastUpdateDate { get; set; } 

    //[DisplayName("Title")] 
    public string Title { get; set; } 

    //[DisplayName("Abstract number")] 
    public int AbstrNum { get; set; } 

    //[DisplayName("Poster title")] 
    public string PosterTitle { get; set; } 

    //[DisplayName("Workshop")] 
    public string Workshop { get; set; } 

    //[DisplayName("Keywords")] 
    public string Keywords { get; set; } 

    //[DisplayName("Institution")] 
    public string Institution { get; set; } 

    //[DisplayName("Collaboration email")] 
    public string CollabEmail { get; set; } 

    public string SessionDate { get; set; } 

    //[DisplayName("DCResults")] 
    public List<string> DCResults { get; set; } 

    public string result { get; set; } 

public List<string> SelectedDCResults { get; set; } 
} 
//public class SelectedDCResults 
//{ 
// public string result { get; set; } 
//} 
}  

查看:

@model BFProj2.Models.OurColumns 

@{ 
    ViewBag.Title = "Importcsv"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Import CSV</h2> 

@Html.EditorFor(model => model.FirstName) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.result, new SelectList(Model.DCResults)) 
</div> 

@Html.EditorFor(model => model.LastName) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.SelectedDCResults, new SelectList(Model.DCResults)) 
</div> 

@Html.EditorFor(model => model.Email) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.SelectedDCResults, new SelectList(Model.DCResults)) 
</div> 

@Html.EditorFor(model => model.UserName) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.SelectedDCResults, new SelectList(Model.DCResults)) 
</div> 

@Html.EditorFor(model => model.Comment) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.SelectedDCResults, new SelectList(Model.DCResults)) 
</div> 

@Html.EditorFor(model => model.Title) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.SelectedDCResults, new SelectList(Model.DCResults)) 
</div> 

@Html.EditorFor(model => model.Workshop) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.SelectedDCResults, new SelectList(Model.DCResults)) 
</div> 

@Html.EditorFor(model => model.AbstrNum) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.SelectedDCResults, new SelectList(Model.DCResults)) 
</div> 

@Html.EditorFor(model => model.PosterTitle) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.SelectedDCResults, new SelectList(Model.DCResults)) 
</div> 

@Html.EditorFor(model => model.Keywords) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.SelectedDCResults, new SelectList(Model.DCResults)) 
</div> 

@Html.EditorFor(model => model.Institution) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.SelectedDCResults, new SelectList(Model.DCResults)) 
</div> 

@Html.EditorFor(model => model.CollabEmail) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.SelectedDCResults, new SelectList(Model.DCResults)) 
</div> 

<!--Add session date to UserInput table as string format--> 

@Html.EditorFor(model => model.SessionDate) 
<div class="display-field"> 
@Html.DropDownListFor(m => m.SelectedDCResults, new SelectList(Model.DCResults)) 
</div> 

回答

0

首先,獲得此代碼你的控制器。在MVC中,控制器應該很薄 - 他們的責任應該是弄清楚哪個視圖可以在視圖和業務層之間進行渲染和傳遞數據。 其次,您的View強類型爲OurColumns對象,OurColumns對象的實例永遠不會傳遞該視圖。它看起來像你需要一個ViewModel,它有一個DropDownList的DCResults集合和一個捕獲選定值的字符串。這並不是絕對必須有一個單獨的「選定」項目,但我喜歡它如何簡化代碼 - 畢竟它是一個ViewModel。

public class ViewModel { 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public string Email { get; set; } 
     public List<string> DCResults { get; set; } 
     public string SelectedDCResult { get; set; } 
    } 

你的控制器應負責創建在GET請求視圖模型和分析視圖模型到業務對象(或過去沿到服務中更加分層應用)。

public ActionResult Get() 
{ 
    var model = new ViewModel { 
     DCResults = this.DcResultRepos.DCResults 
    }; 
    return View(model); 
} 

public ActionResult Post(ViewModel model) { 
    if (ModelState.IsValid) { 
     //do something 
    } 
    return View("Another View"); 
} 

如果不使用持久性存儲的ORM到對象綁定,那麼您將需要創建自己的,另一類這樣你就可以抽象掉,從WebUI的結合。對於前面的例子中,我用下面的簽名:

private class DCResultRepository { 
    public List<string> DCResults { get; } 
} 

在GET(或helper方法)的身體,你可以處理你的數據表和使用收益率迴歸到創造一個IEnumerable將允許您使用中的LINQ你的控制器。像這樣:

private DataTable table; 
public IEnumerable<Model> DCResults { 
    get { 
     //do something to get datatable 
     foreach(var row in table.Rows){ 
      yield return new Model(){ 
       //initialize values 
      }; 
     } 
    } 
} 
相關問題