user8205798
user8205798

Reputation:

Drop down list binding through ODBC in ASP.NET MVC and MySQL

My problem:

Trying to establish a connection through ODBC from visual studio (ASP.NET MVC project) to a remote server (Fishbowl) which uses MYSQL. Ultimate goal is to bind data from database to the drop down list. Instead of actually retrieving the rows from the column in query, I get output like this when I run the code:

enter image description here

Where am I going wrong? Any suggestions or alternate method? Thanks!

Controller Connection Part

DataSet ds = new DataSet();

        using (OdbcConnection connfb = new OdbcConnection("DSN=Fishbowl"))
        {
            using (OdbcCommand cmd = new OdbcCommand("select xyz from abc", connfb))
            {
                connfb.Open();
                cmd.ExecuteNonQuery();

                OdbcDataAdapter da = new OdbcDataAdapter(cmd);
                //da.Fill(dt);
                da.Fill(ds);

                List<AddInventoryModel> refNumList = new List<AddInventoryModel>();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    AddInventoryModel robj = new AddInventoryModel();
                    robj.referenceNumber = ds.Tables[0].Rows[i]["num"].ToString();
                    refNumList.Add(robj);
                }
                Adi.refNumDD = refNumList;
            }
            connfb.Close();

Model

 public class AddInventoryModel
    {
    [Display(Name = "1. Reference Number: ")]
        [Required(ErrorMessage = "Reference Number is required!")]
        public string referenceNumber { get; set; }

    public List<AddInventoryModel> refNumDD { get; set; }

View

@Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.referenceNumber, htmlAttributes: new { @class = "control-label col-md-100" })
        <div class="col-md-20">
            @Html.DropDownListFor(model => model.referenceNumber, new SelectList(Model.refNumDD, "referenceNumber"), "Choose reference number")
        </div>
    </div>
}

Upvotes: 0

Views: 290

Answers (1)

Shyju
Shyju

Reputation: 218762

You are using the incorrect overload of SelectList constructor.

Use this overload

public SelectList (System.Collections.IEnumerable items, 
                   string dataValueField, 
                   string dataTextField);

The second parameter is the value field which will be used to set the OPTION value attribute value and the third parameter is the text filed which will be used to render the OPTION text.

Since your controller code is only setting the referenceNumber property on the
AddInventoryModel objects, you can use that for both dataTextField and dataValueField.

@Html.DropDownListFor(model => model.referenceNumber, 
                       new SelectList(Model.refNumDD, "referenceNumber","referenceNumber"),
                       "Choose reference number")

For rendering SELECT elements, I personally prefer to use a collection SelectListItem in my view model, instead of a collection of custom objects.

Upvotes: 1

Related Questions