Here is my code:
UserFacilities is my list for the dropdownlist and User_Facility_ID would be the selected item.
This is the Get method for the grid which populates the ViewModel. I never get to the POST. It throws the error as soon as I click the "Add New Record" button on the grid.
This is the DropDownListFor in my EditorTemplate being used on the grid.
I feel like I am missing something simple here...like somehow initializing the User_Facility_ID to an empty string or something so on a new record it doesn't have a null value which it is not allowed to have. I have tried doing this in the ViewModel but unless I was doing it wrong it didn't help.
14 Answers, 1 is accepted
I would recommend you to check the following Code Library project:
I was testing that out using the Kendo DropDownListFor and what I realized is that its not my selected id being null that's a problem, it's that my selectlist itself is coming in null.
So the "Value cannot be NULL" error is actually being thrown on the Model.UserFacilities property:
So I guess it is the way it is being filled? If I debug, after the "Add New" is clicked on the grid and it goes back to my get method, I see that the UserFacilities property of my ViewModel is being filled properly, but somehow when it gets back to the grid it becomes null...??
Here is what I have now in my get method for the grid:
I am doing that sort of nested select in the LINQ projection to fill the UserFacilities SelectList. Is there another way I can do it without using the ViewData/ViewBag? Again, this works on an edit, just not on an add.
Could you please provide a runnable project where the issue is reproduced? This would help us pinpoint the exact reason for this behavior.
Once you run it there will be a link on the Index page called "Proposal Types". Click that and it takes you to the view with the grid. If you edit a record the User Facility dropdown works fine. If you try to add a new one you will get the error message.
I had to remove the "packages" and the "lib" folders from the solution folder as it was too large to upload. I'm not sure if those are necessary or if you have the references locally it will run anyway.
Thank you for the provided project. I examined it and noticed that User_Facility_ID DropDownList is populated from the Model. I am not sure what is the reason for using the Model instead of the ViewData to pass the UserFacilities list, however I would like to point out that when adding a new item all of its fields are null. This includes the UserFacilities field which is used for populating the DropDownList. I would recommend you to pass the data through the ViewData object, ensuring it will always be there, as shown in the example project I sent you in one of my previous replies.
This is quite an old thread which focuses on ServerEditing.
I tested a server editing scenario and had no issues passing the model collection around both to the Kendo UI DropDownList and to the DropDownList provided by the framework:
However, few projects these days use server rendering and editing, so I assume that the desired functionality is relevant to the Ajax bound grid.
Using the ajax bound grid, the standard approach is to provide a data source or use the ViewData as it is done in the Foreign Key scenarios. If you wish to bind to the model items, there is one way to achieve it. During editing the Kendo UI MVVM framework is called upon to initialize the widgets on the form. So you may decorate the DropDownList with a data-bind attribute which will bind the available model collection to the data source:
Let me know in case you have further questions.