0

I have issue with correct bind SelectListItem with a view.

ItemController.cs

 public ActionResult SelectCondition()
        {

            List<SelectListItem> items = new List<SelectListItem>();

            items.Add(new SelectListItem { Text = "New", Value = "0", Selected=true });

            items.Add(new SelectListItem { Text = "Old", Value = "1" });

            var model = new Item
            {
                ItemCondition = items
            };

            return View();

        }

Create.cshtml

@Html.DropDownList("SelectCondition", (IEnumerable<SelectListItem>)Model.ItemCondition)

Item.cs

public IEnumerable<SelectListItem> ItemCondition { get; set; }

Now I have NullReferenceException and underlined this line in Create.cshtml

Prezes Łukasz
  • 938
  • 1
  • 9
  • 30
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Aug 16 '15 at 07:19

1 Answers1

0

Isn't it very suspicious for Visual Studio that var model is declared but never used?

Pass the model to the view. return View(model).

And general suggestion: Work with strongly typed views...

Royal Bg
  • 6,988
  • 1
  • 18
  • 24
  • i pass the model to the view, but still have same problem - NullReferenceException :( – Prezes Łukasz Jun 22 '15 at 21:15
  • Make `ItemConditiion` property in `Item` class the respective type, then tell your View that your model is of type `Item` and remove the cast. – Royal Bg Jun 22 '15 at 21:17