2

After doing some upgrades to my project, suddenly my generated "_createoredit.cshtml" file is blowing up with this:

The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.Int32'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.Int32'.

Source Error:

Line 273:</div> 
Line 274:<div class="editor-field"> 
Line 275:@Html.EditorFor(model => model.NumOfStores)
Line 276:@Html.ValidationMessageFor(model => model.NumOfStores)
Line 277:</div>

The field it references is a nullable int type. This just worked before (the production version is still running fine), and the changes I made to the project shouldnt have touched anything this uses, so now i can't figure out why the heck this is blowing up.

update 1- adding the field def from the model

[DisplayName("Number of Stores (if applicable)")]
public Nullable<int> NumOfStores { get; set; }

Also I have verified that all the nullable value types are having this same issue.

Brady Moritz
  • 8,624
  • 8
  • 66
  • 100

2 Answers2

1

Try specifying a name for your template.

@Html.EditorFor(model => model.NumOfStores, "NumStoresTemplate")

More information (from MVC 2 but I think it applies to your situation) http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html

mattypiper
  • 1,222
  • 8
  • 8
  • Also - the model.NumOfStores is nullable int type, so should be fine there. This is my first time to mess with mvc forms and templates, it's blowing my mind how fragile and just.. bad they are. – Brady Moritz Nov 29 '11 at 17:08
  • also - null is a legal value for me, and zero is another value, so I can't default it to zero. – Brady Moritz Nov 29 '11 at 17:09
  • Sorry, I think I misunderstood the question. Do you have any custom templates installed at ~/Views/Shared/EditorTemplates? It appears to be an issue in binding the nullable model item to the view. – mattypiper Nov 29 '11 at 17:43
  • Also, can you post the code of your ViewModel? You do have a nullable int in your model definition, correct? I think you have a mismatch between what you pass to the view and what the view expects. – mattypiper Nov 29 '11 at 17:46
  • [DisplayName("Number of Stores (if applicable)")] public Nullable NumOfStores { get; set; } – Brady Moritz Nov 29 '11 at 18:08
  • Unfortunately I cannot reproduce this issue in a demo I put together using nullable value types and default edit templates. If you comment out lines 275 and 276 does the issue go away? Have you tried setting breakpoint directly in the view at 275 and inspecting the model using Visual Studio's debugger? Sorry, just trying to come up with ideas to help you at this point. – mattypiper Nov 29 '11 at 20:37
  • I don't have any custom editor templates created (I'm looking to make sure there arent some hiding in this larger project). – Brady Moritz Nov 29 '11 at 21:16
  • I commented out this item, and it errors then on the next nullable int field. Commented it, and then it errors on the next nullable double field. so it appears that any nullable value types are blowing up. Just crazy... – Brady Moritz Nov 29 '11 at 21:18
  • Did you try specifying the template name like my answer suggested? Also, double check your view Inherits="ViewPage" is setup correctly. – mattypiper Nov 29 '11 at 21:32
  • I have this same project open in an earlier version, running it, and it works fine. So something in this new version is messing it up. I'll have to try isolating some things it seems... – Brady Moritz Nov 30 '11 at 15:03
0

I wound up adding custom editor templates for int32 and double to get this to work. I realized the project I'm working with has telerik controls added to it, so thought I'm not sure how to debug things, it appears they are getting involved with the default templates somehow.

Brady Moritz
  • 8,624
  • 8
  • 66
  • 100