0

I am trying to pull the data from a table stored in SQL 2008 into my MVC4

In My Controller :

 public ActionResult Test()
    {
       SurveyEntities survey = new SurveyEntities();

       var doctorList = survey.Doctors.ToList();
       return View(doctorList);
    }

and in my View:

@model IEnumerable<Survey.DataAccess.Doctor> 

@{
    ViewBag.Title = "Test";
}

<h2>Test</h2> 

@using (Html.BeginForm("Test", "Home", FormMethod.Post))
{
  @Html.DropDownListFor(m => m.)
}

But I am not able to access the field name in m, say for eg., the doctor's name, to bind it to the dropdownlist.

Where am i going wrong ?

Ron
  • 1,901
  • 4
  • 19
  • 38
  • Your model is a collection. A collection does not expose the properties of the type it contains. – CodeCaster Jan 23 '17 at 21:38
  • A dropdownlist requires 2 properties - a property to bind the selected value to, and a collection for the options (of type `IEnumerable`). –  Jan 23 '17 at 21:38
  • It difficult to know what you want to achieve here (does `Doctor`represent the options, or are you creating a dropdownlist for each `Doctor`) - suggest your read [this question/answer](http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) for a typical implementation –  Jan 23 '17 at 21:42
  • @StephenMuecke I am trying to populate the name of the doctors in the dropdown list pulling from the database – Ron Jan 23 '17 at 21:43

3 Answers3

1

If you dont need to bind to the result value you can also use

Html.DropDownList('name', new SelectList(Model))

If you have to use DropDownListFor you would have to change your model and add a property to bind the select result like

Html.DropDownListFor(m=>m.DoctorId, new SelectList(Model.Doctors).....

Ivaylo Stoev
  • 457
  • 3
  • 7
1

Normally, you want to use ViewModel, so that you can retrieve the selected doctorId when the form is posted back to server.

For example,

enter image description here

Model

public class SurveyModel
{
    public string SelectedDoctorId { get; set; }
    public IList<SelectListItem> AvailableDoctors { get; set; }

    public SurveyModel()
    {
        AvailableDoctors = new List<SelectListItem>();
    }
}

View

@model DemoMvc.Models.SurveyModel
@using (Html.BeginForm("Index", "Home"))
{
    @Html.DropDownListFor(m => m.SelectedDoctorId, Model.AvailableDoctors)
    <input type="submit" value="Submit" />

}

Controller

public ActionResult Index()
{
    var model = new SurveyModel
    {
        AvailableDoctors = GetDoctorListItems()
    };
    return View(model);
}

[HttpPost]
public ActionResult Index(SurveyModel model)
{
    if (ModelState.IsValid)
    {
        var doctorId = model.SelectedDoctorId;
        // Do something
        return View("Success");
    }
    // If we got this far, something failed, redisplay form
    // Fill AvailableDoctors again; otherwise, DropDownList will be blank.
    model.AvailableDoctors = GetDoctorListItems();
    return View(model);
}

private IList<SelectListItem> GetDoctorListItems()
{
    /*
    SurveyEntities survey = new SurveyEntities();
    return survey.Doctors
        .Select(d => new SelectListItem {Text = d, Value = d.ToString()})
        .ToList();
    */

    // Simulate doctors return from database.
    return new List<SelectListItem>
    {
        new SelectListItem {Text = "John Doe", Value = "1"},
        new SelectListItem {Text = "Eric Newton", Value = "2"}
    };
}
Win
  • 61,100
  • 13
  • 102
  • 181
1

you can put this code in the Test method:

> ViewData["doctors"] = new SelectList(doctorList,"value", "text");

and then in a view:

     @using (Html.BeginForm("Test", "Home", FormMethod.Post)) 
{
     @Html.DropDownList("name", ViewData["doctors"] as SelectList) 
     input type="submit" value="Submit" />

}