How to correct bind SelectListItem with a view?

395 views Asked by At

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

1

There are 1 answers

2
Royal Bg On

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...