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

Im new for MVC, and when I built the model classes for the first code approach, each key identifier column had the [ScaffoldColumn(false)] attribute. I did this because I do not need the identifiers displayed in the views, obviously, and I know that working on this problem will remove the HTML generated by the user interface in each view containing the identifier field.

The problem I encountered when using the ScaffoldColumn set to false in models was first noticed when the Edit method for the view was canceled, and I got this error: the Store application update, insert or delete fixed unexpected number of rows (0). When I ran the code in debugging, the ID property was actually set to zero, as indicated in my Edit HttpPost method.

My question, to which I hope someone can think about whether it makes sense to have an attribute of type ScaffoldColumn in MVC, if the identifier is not sent to the controller method, as in this case Edit HttpPost ? Should I just use this old fashion method and remove the HTML markup in every view created by MVC temples generated when this attribute is not added to the key / foreign key properties?

The work I discovered if you set this attribute to false added an ID parameter to each HttpPost method, rather than setting the corresponding ID property field before calling SaveChanges() .

+6
source share
3 answers

Well, you don’t want it to be built as an editable field, but, of course, you want it in the form, as you noticed. The solution is to simply add it manually as a hidden field in the view. Something like that:

 @Html.HiddenFor(model => model.id) 
+2
source

You can use data annotations for this and work with:

 [HiddenInput(DisplayValue=false)] 

Thus, your field will be displayed using hidden input in the view. There is no need to make any changes, the view will know how to display it.

+3
source

ScaffoldColumn changes the behavior of methods, such as Html.DisplayForModel (), which actually use the standard template system introduced in MVC 2 . This does not affect the Visual Studio wizards that ship with MVC.

0
source

Source: https://habr.com/ru/post/915159/


All Articles