last day (15 days later) » 

4:27 PM
0
A: Can't get DropdownListFor to work properly

ekadSince you get the error when you save the form, I think the problem is inside the Create method with [HttpPost] attribute below. [HttpPost] public ActionResult Create(Song songToCreate) { try { db.Songs.Add(songToCreate); db.SaveChanges(); return RedirectToAction(...

 
Ok so now instead of crashing it just reloads the page and doesn't save anything. Doesn't address the underlying issue, which has to do with how I'm creating the ViewBag select list and/or the DropDownListFor line (which is where the crash occurs).
 
How you create the ViewBag and use DropDownListFor is fine, the error is because you didn't repopulate ViewBag.GenreSelect when returning to the same page after saving the form.
 
The form does not save.
 
Now we need to find out the error that happens inside the try block. Change the catch block to catch (Exception ex), then debug and see what the value of ex.Message is.
 
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
 
4:27 PM
Can you add the code of Song class and also the value of all songToCreate properties when Create HttpPost method is executed?
 
Added the song class. Not sure what you mean by the value of all songToCreate properties.
ok
 
I've seen the Song class, but what's the value of songToCreate.Id and songToCreate.Name inside Create method?
is there any restrictions in the database for Id or Name column?
 
the Id and name are from the genre table
name is just for display
Id links the tables
 
no that's not what I meant
did you debug inside Create [HttpPost] method?
 
Sure
 
4:33 PM
and check the value of songToCreate.Id and songToCreate.Name
I think you also need to check EntityValidationErrors properties as suggested by the error message.
 
I can't figure out how to check that
 
do you even know how to debug?
 
yes
The song's name and Id are not at all related to the genre name and Id
it has its own fields for that
They're fine.
 
what's the value of id and name that you entered?
 
I don't enter Id
and name is a separate field
there's a box for a name
and then there's the genre, which you select from a dropdown
which displays a name but saves the genre's id
the song's name has nothing to do with this
Maybe something related to the overlapping names of the properties I don't know
It was working fine before though so I don't have a clue.
 
4:40 PM
ok can you check EntityValidationErrors property then? what's the message in there?
 
I changed the catch to catch (DbEntityValidationException e)
but I don't get how to print the errors
 
don't print the errors, just put a break point inside the catch block and see its properties
 
oops.
 
I added a required field but didn'
t add it to the form
That's what the error is showing
 
4:52 PM
ok do you know how to fix it?
 
Now new error, yay
Well I added the field to the form and didn't leave it blank
DbUpdateException now
 
what does the error message say?
 
These layers of exceptions are so annoying lol
A first chance exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll
An error occurred while updating the entries. See the inner exception for details.
 
then check the inner exception
 
yea that was the inner exception
so i did .InnerException.InnerException.Message
which worked
apparently something to do with the foreign key constraint
conflict
Presumably because the genreId isn't being set properly
which is used as a foreign key
 
4:58 PM
I guess you should fix it yourself since you already know the tables structure.
 
Yea
But I think it's because the genreId is null
which brings me back to the original issue of what's up with the dropdown
 
the Song class only has Id and Name property
where does genreId come from?
did you post the wrong code of Song class?
 
Yea I had to simplify it for the question and I accidentally left it out
Sorry
Fixed that
 
when you do @Html.DropDownListFor(model => model.GenreId, that means the selected value of the dropdown will be assigned to GenreId property.
 
OK, that's the main thing I was unclear about
 
5:04 PM
did you choose the genre from the dropdown or not?
 
It works now
Thanks
Seems to me that the issue was between the missing required field which you obviously had no way of knowing about so my bad about that) and the misunderstanding of how the DropDowListFor first parameter works (which was my initial question)
 
ur welcome, I'll edit my answer to address that
 
Because even if I had the right stuff in DropDownListFor I would keep getting the error
that is completely unrelated, and think it was because of the DropDownListFor
(maybe)
 
ok so are you clear about DropDownListFor now?
 
I think so; I was just confused as to whether the first parameter was referring to the item or the model
 
5:17 PM
I think you should read this: stackoverflow.com/q/7142961/1905949
 
OK
 

last day (15 days later) »