我很困惑如何做到這一點,我看到這些: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>