How do I bind a datatable to model and then to a d

2019-09-01 06:34发布

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>

1条回答
在下西门庆
2楼-- · 2019-09-01 07:15

First, get this code out of your controller. In MVC controllers should be thin – their responsibility should be figuring out which View to render and passing data to and from the Views and the business layer. Second, your View is strongly typed to the OurColumns object an instance of the OurColumns object is never passed the view. It looks like you need a ViewModel that has a collection of DCResults for your DropDownList and a string to capture the selected value. It is not strictly necessary to have a separate “Selected” item, but I like how it simplifies code – it’s a ViewModel after all.

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; }
    }

Your controller should be responsible for creating the View Model on the GET request and parse the ViewModel into the Business Object (or past that along to a service in a more layered application).

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");
}

If you are not using an ORM for persistent storage to object binding, then you will need to create your own, in another class so you can abstract away that binding from the WebUI. For the previous example, I used the following signature:

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

In the body of the get (or helper method) you could process your DataTable and use yield return to create an IEnumerable that would allow you to use Linq in your controller. Like this:

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
            };
        }
    }
}
查看更多
登录 后发表回答