views:

3190

answers:

4

This error message is driving me nuts.

I'm getting it when using Html.ListBox and Html.DropDownList HtmlHelpers with ASP.NET MVC v1.0. Populating the lists works OK - I can view them, etc - but when I go to create a new record in the Model using the FormCollection passed into the Controller to get the selected value, this error occurs. I've tried several different configurations and code arrangements as per several other posts on this matter - no joy.

Hopefully someone can tell me whether this is a known issue or not so as I can move on with my life! Would prefer not to have to post code if necessary, but can do if requested.

Oh, and a suggested workaround/solution would be nice too ;-)

TIA!

Bernard.


Ok, quick update, I've posted three comments to responses below clearing up details as to what I'm up to here.

I now have another issue to share - I've tried ScottGu's alternative recommended approach - implementing a ModelView. So, when I do this it's basically the same scenario, but I get an "Object reference not set to an instance of an object." error at the same place that I was getting "There is no ViewData item with the key 'Blah' of type 'IEnumerable'.".

I'm starting to thing that this is perhaps something to do with the way the EF works...

A: 

Not knowing how you are coding this will be hard for people to identify the problematic area. Have a read of this forum thread - further down has a similar error to you.

http://forums.asp.net/p/1320426/2623986.aspx

David Liddle
Ok, apologies, I have not provided much detail - I was hoping that this is a fairly widely encountered issue. Basically, what I'm trying to do is what ScottGu talks through here:http://nerddinnerbook.s3.amazonaws.com/Part6.htmI want to POST a 'Create' or 'Edit' result back to the Controller.
Bernard
The error occurs at "work.Task = dataSourceTask.GetTask(taskID);".I'm using Entity Framework. The Job and Task properties of the Work structure are EntityReferences.The bizzare thing is that everything works fine up to the point of the crash. My jobID and taskID variables are populated, work.Job is populated fine, the error always occurs when I go to do _anything_ beyond populating the first referenced structure (yep, I've tried manipulating the code to do other stuff, populate the Task first, etc - always the same error).Hope you can help!Cheers,Bernard.
Bernard
A: 

Ok, sooo I'm clearly an EF/MVC noob...

Although the issue was solved by my actually implementing proper exception management...the exception message (when I actually started catching it) was "The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.". So I'm using the same ObjectContext for all data access now, and things are back on track.

Thanks for caring, I'll have a smarter question next time - promise!

Bernard.

Bernard
A: 

You can again bind your select list in the according post method where validation occurs. For example you have an error inside the create view of the controller than you can again bind dropdown list in create post method.

Stuti
A: 

Hello Bernard,

i am having the same problem!!! when i populate a selectList manually and set it to ViewDate["something"] (for example a true or false select list) i get that error right away when i hit the submit button even though i see the list created OK!... i fixed this by creating the list in the view... which im not supposed to do, but anyway...

is Product Discontinued: <%= Html.DropDownList("ProdDiscontinued", new SelectList( new [] { "false", "true"} ) ) %>

So i have 2 other cases like this.. for example Product categories and Owner of the product... and this is where my other problem comes.. Product is created OK when i fill all the data correctly (numbers as 1.2.3...4 etc) and fill all the fields... but when i dont fill one of them or fill it wrong... i get an exception like this

There is no ViewData item with the key 'ProductCatID_FK' of type 'IEnumerable'.

Does anybody has a suggestion for this?

this is the code in the view

Product Category: <%= Html.DropDownList("ProductCatID_FK", ViewData["ProductCategories"] as SelectList) %> <%= Html.ValidationMessage("ProductCatID_FK", "*")%>

And the code in the Controller class

ViewData["ProductCategories"] = new SelectList(productCategoriesRepository.FindAllProductCategories(), "ProductCatID", "ProductCatName");

and the code in the Repository

    public IEnumerable<ProductCategory> FindAllProductCategories()
    {
        return db.ProductCategories;
    }

thanks a lot in advance

Eduardo