0

I can't seem to figure out what is wrong with my code. I want to have a drop down list that displays Director names instead of the IDs. Works perfectly fine if I leave the dropdown out.

The troubling part in the view "Create":

<div class="editor-field">
    @Html.DropDownListFor(model => model.DirectorID, Model.GetDirectors())
    @Html.ValidationMessageFor(model => model.DirectorID)
</div>

The corresponding method in the model "Movies":

public IEnumerable<SelectListItem> GetDirectors()
{
    var directors = db.Directors.ToList();
    IList<SelectListItem> items = new List<SelectListItem> { };
    foreach(var item in directors)
    {
        items.Add(new SelectListItem()
        {
            Value = item.DirectorID.ToString(),
            Text = item.People.Firstname + " " + item.People.Lastname
        });
    }
    return items;
}
user1826176
  • 309
  • 2
  • 14
  • 1
    Can we see the *StackTrace*, the code look corect, the `db` or `Directors` may be null. You create an instance of `Movie` and pass it to View? If you can show as the Action Create will be awesome. The `item.People` can be null also. – adricadar Apr 09 '15 at 11:28
  • 1
    `People` and `DirectorId` can also be null. – Jenish Rabadiya Apr 09 '15 at 11:30
  • 1
    Its basic exception You can fivure it out – Ehsan Sajjad Apr 09 '15 at 11:39
  • 1
    Have you initialized a new instance of your model and passed it to the view - `return View(model);` - otherwise `Model` would be `null` and therefore `GetDirectors` would throw the exception. –  Apr 09 '15 at 11:48
  • 1
    @user1826176, That link is a POST method, not a GET method. –  Apr 09 '15 at 11:53
  • 1
    You don't have a GET method? How do you generate the initial view? - `public ActionResult Create() { Movies model = new Movies(); return View(model); }` But your model should not contain that method anyway. Use a property for the `SelectList` and assign the value in the controller. –  Apr 09 '15 at 12:09
  • 1
    And you need to rethink your POST method and the way you assign the ID. Use an auto-incremented database property. You existing implementation will fail if 2 users create a new movie at the same time. –  Apr 09 '15 at 12:12
  • @stephen Figured it out, sorry to have wasted your time – user1826176 Apr 09 '15 at 14:39

1 Answers1

0

In the model I have the following properties

    [Display("DirectorName")]
    public int DirectorID { get; set; }
    public IEnumerable<SelectListItem> DirectorList { get; set; }

Bellow code in DirectorDA.cs class

 public IEnumerable<Director> GetDirector()
        {
            return (from dir in db.Director select cus).AsEnumerable();
        }

And the following code in my controller

DirectorDA director = new GetDirector();
var directors = director .Director();
List<SelectListItem> items = new List<SelectListItem> { };
            foreach (var item in directors)
            {
                items.Add(new SelectListItem()
                {
                    Value = item.DirectorID.ToString(),
                    Text =  item.Name
                });
            }

Bellow code in my View

@Html.LabelFor(model=>model.DirectorID)
@Html.DropDownListFor(model=>model.DirectorID, Model.DirectorList)

It's working for me, But I have one table for Director. Can you try to join the table before assign to List.

Dino
  • 199
  • 2
  • 13