[MVC 3]– HiddenFor Helper –Remember to Use!–TIP


I was skyping a MVC problem with my friend Shaw Young the other day, and it turned out the problem i was experiencing was a nOOb error of not passing a my from the view to the Model, and i kept getting the following SQL Foreign Key exception:

 

The INSERT statement conflicted with the FOREIGN KEY constraint FK_source_destination

 

 

This turned out to be a red herring. The issue was that my View hadn’t had my Guid  field Unique_Key declared to be bound, ready for POST Action Method. Now because i don’t want the user to see the keys (It would be ugly!) on the view, you can use the following HTML Helper method:

 

@Html.HiddenFor(model => model.Unique_Key)

 

This will mean my Guid is not rendered to the user BUT is passed to my POST method to be used without throwing any nasty exceptions. A nOOb mistake but easy to miss and even harder to find! I spent along time checking my SQL DB Design.

Enhanced by Zemanta

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s