There is no ViewData item of type 'IEnumerable' that has the key 'PestType'

asp.net-mvc-4 c# entity-framework enums

Question

I am learning MVC and Entity Framework, so pleas bear my questions... I am trying to create a drop down menu for a property (Enum type) for my model class SinglePest Here is the model:

public class SinglePest
{
    public int SinglePestId { get; set; }
    public PestType PestType { get; set; } // here is my problem
    public string Alias { get; set; }
    public string  TechName { get; set; }
    public string Markings { get; set; }

    public string SerialNumber { get; set; }
    public SourceType SourceType { get; set; }

    //virtual property
    public Source Source { get; set; }
}

Here is PestType:

    public enum PestType
{
    Dog,
    Cat,
    Fox,
    Rabbit,
    Rat
}

This is the controller:

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create (//(SinglePest singlepest)
    [Bind(Include= "Alias, TechName, SerialNumber, PestType, Markings")] SinglePest singlepest)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.SinglePests.Add(singlepest);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        catch (DataException /* dex */)
        {

            ModelState.AddModelError("", "Unable to save changes, try again, if problem   persits contact your administrator");
        }

        //ViewBag.SerialNumber = new SelectList(db.Sources, "SerialNumber", "SerialNumber", singlepest.SerialNumber);
        return View(singlepest);
    }

And here is the View where I get the error (There is no ViewData item of type 'IEnumerable' that has the key 'PestType'.) :

<div class="editor-label">
        @Html.LabelFor(model => model.PestType, "Pest Type")
    </div>
    <div class="editor-field">
        @Html.DropDownList("PestType", String.Empty) // here is the error
        @Html.ValidationMessageFor(model => model.PestType.ToString())
    </div>

Now I have seen some posts about displaying enum, but I can't figure out a solution for my problem. Could please someone give me some piece of advice on how to fix it?

Thank you for your time!

1
2
9/13/2013 8:47:53 AM

Accepted Answer

You have @Html.DropDownList("PestType", String.Empty), but the second param needs to be an IEnumerable<T>. You will need the list of your pest in the model, and then use model.Pests for example where Pets is an IEnumerable.

EDIT: Based on comment...

But I want to display just the various types of pest (Dog, Cat, etc) not all the pests that are in my database

OK, are these categorised, could you write something like (hand written so check syntax)..

var pests = (from _context.Pests.Where(p => p.CategoryId == 1) select p.PestName).ToList();

If you need to get a IEnumerable for the enum (since I'm not sure what the DB looks like), you can use...

Enum.GetValues(typeof(PestType))
            .OfType<PestType>()
            .Where(p => p == ??);
4
9/13/2013 9:12:09 AM

Popular Answer

Currently your model only contains a place to store the value and not a Array/IEnumerable to populate the drop down from.

First add an IEnumerable to your model:

public class SinglePest
{
    public int SinglePestId { get; set; }
    public IEnumerable<PestType> Pests { get; set; }
    public PestType PestType { get; set; }
    public string Alias { get; set; }
    public string  TechName { get; set; }
    public string Markings { get; set; }

    public string SerialNumber { get; set; }
    public SourceType SourceType { get; set; }

    //virtual property
    public Source Source { get; set; }
}

And in your controller:

public ActionResult Create()
{    
    var model = new SinglePest();

    model.Pests = Enum.GetValues(typeof(PestType)).Cast<PestType>()

    return View(model);
}

And your view:

@Html.DropDownListFor(m => m.PestType, Model.Pests);

Sorry if theres any errors I've written this from memory...



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow