Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Real Advantage to ScaffoldColumn Attribute set to False in MVC 3 Model ID Properties

I’m new to MVC and when I built my model classes for a code first approach each key ID column had the attribute [ScaffoldColumn(false)] set. I did this because I didn’t want the IDs displayed in the views obviously, and I know that a work around to this problem would be deleting the HTML generated by the UI in each view that contains an ID field.

The problem I ran into by using ScaffoldColumn set to false in the Models was first noticed when an Edit method for a view was fired off, and I received this error: Store update, insert, or delete statement affected an unexpected number of rows (0). When I ran the code in debug the ID property was in fact set to zero as indicated in my view Edit HttpPost method.

My Question that I hope someone might elaborate on, is what’s the point in having an attribute like ScaffoldColumn in MVC if the ID is not being sent to a controller method(s) like in this case an Edit HttpPost? Should I just go about this the old fashion way, and delete the HTML markup in each view created by the MVC temples generated when this attribute is not added to a key/foreign key properties?

The work around I found if you set this attribute to false was adding an ID param to each HttpPost method, and than setting the appropriate ID property field prior to calling SaveChanges().

like image 591
Shawn Avatar asked May 08 '12 05:05

Shawn


1 Answers

You could use data annotations for this and work with:

[HiddenInput(DisplayValue=false)]

This way, your field will be displayed using a hidden input into the view. No need to make any changes, the view will know how to display it.

like image 167
Simply Me Avatar answered Oct 06 '22 05:10

Simply Me