如何将数据表绑定到模型,然后再绑定到 mvc 中的下拉列表?
How do I bind a datatable to model and then to a dropdownlist in mvc?
我对如何实现这一点感到非常困惑,我看到了这些:reference to similar question 1 & reference to similar question 2 None 这两个问题的答案解释了我如何将实际的数据表变量绑定到模型,因为它们是指定的更多到下拉列表部分。
我尝试使用两种方法:一种是将模型中的列表绑定到数据表字符串,另一种是创建一个模型 class,其中包含我通过创建列表来绑定的字符串控制器模型中的字符串对象。但我的问题仍然是数据表变量和它传递给的模型项在 foreach 中消失了。
那么我应该如何正确使用模型将数据表绑定到下拉列表?
控制器:
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>
首先,从您的控制器中获取此代码。在 MVC 中,控制器应该很薄——它们的职责应该是确定要呈现哪个视图,以及在视图和业务层之间传递数据。
其次,您的视图是 OurColumns 对象的强类型,OurColumns 对象的实例永远不会传递给视图。看起来您需要一个 ViewModel,它包含 DropDownList 的 DCResults 集合和一个用于捕获所选值的字符串。有一个单独的“Selected”项并不是绝对必要的,但我喜欢它简化代码的方式——毕竟它是一个 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 进行持久存储到对象绑定,那么您将需要在另一个 class 中创建自己的对象绑定,这样您就可以从 WebUI 中抽象出该绑定。对于前面的示例,我使用了以下签名:
private class DCResultRepository {
public List<string> DCResults { get; }
}
在 get(或辅助方法)的主体中,您可以处理您的 DataTable 并使用 yield return 创建一个 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
};
}
}
}
我对如何实现这一点感到非常困惑,我看到了这些:reference to similar question 1 & reference to similar question 2 None 这两个问题的答案解释了我如何将实际的数据表变量绑定到模型,因为它们是指定的更多到下拉列表部分。
我尝试使用两种方法:一种是将模型中的列表绑定到数据表字符串,另一种是创建一个模型 class,其中包含我通过创建列表来绑定的字符串控制器模型中的字符串对象。但我的问题仍然是数据表变量和它传递给的模型项在 foreach 中消失了。
那么我应该如何正确使用模型将数据表绑定到下拉列表?
控制器:
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>
首先,从您的控制器中获取此代码。在 MVC 中,控制器应该很薄——它们的职责应该是确定要呈现哪个视图,以及在视图和业务层之间传递数据。 其次,您的视图是 OurColumns 对象的强类型,OurColumns 对象的实例永远不会传递给视图。看起来您需要一个 ViewModel,它包含 DropDownList 的 DCResults 集合和一个用于捕获所选值的字符串。有一个单独的“Selected”项并不是绝对必要的,但我喜欢它简化代码的方式——毕竟它是一个 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 进行持久存储到对象绑定,那么您将需要在另一个 class 中创建自己的对象绑定,这样您就可以从 WebUI 中抽象出该绑定。对于前面的示例,我使用了以下签名:
private class DCResultRepository {
public List<string> DCResults { get; }
}
在 get(或辅助方法)的主体中,您可以处理您的 DataTable 并使用 yield return 创建一个 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
};
}
}
}