I am very confused by how to accomplish this, I have seen these: reference to similar question 1 & reference to similar question 2 None of these two question answers explains how I bind the actual datatable variable to the model since they are specified to the dropdownlist part more.
I have tried using two methods: One was to bind a list from a model to the datatable string and the other was to create a model class with a string that I bind by creating a list of the string object in the model in the controller. But my problem remains in that the datatable variables and the model item it is passed to dies inside the foreach.
So how should I use the model correctly to bind the datatable to the dropdownlist?
Controller:
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 is the datatable.
Model:
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; }
//}
}
View:
@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>