8

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().

John Smith
  • 7,243
  • 6
  • 49
  • 61
Shawn
  • 509
  • 3
  • 12
  • 25
  • The fellow in this blog explains how to use ScaffoldColumn. If you're more of a visual guy like me you'll like the post. It has screenshots, video and all. http://20fingers2brains.blogspot.se/2013/03/scaffoldcolumn-dataannotation-attribute.html – PussInBoots May 07 '13 at 20:12

3 Answers3

4

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.

Simply Me
  • 1,579
  • 11
  • 23
3

Well, you don't want it scaffolded as an editable field, but of course you do want it in the form, as you noticed. The solution is to just add it manually as a hidden field in the view. Something like this:

@Html.HiddenFor(model => model.id)
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • 1
    This is a great work around that I did not think of but what is the common way most MVC programmers deal with hiding key properties in views, or is your example the standard way when setting Scaffolding to off and then add a hidden field on the view? Thanks... – Shawn May 08 '12 at 05:46
  • 1
    Not sure what the standard way is, but that's how I do it. It is kind of surprising that there's apparently no way to scaffold a field as hidden. You *can* make **EditorFor** render a property as a hidden field, however: http://stackoverflow.com/questions/4542972/asp-net-mvc-hidden-field-from-data-annotations – McGarnagle May 08 '12 at 05:55
  • 1
    ... adding, I think in general scaffolding isn't meant to be production-ready, just a way to get something functional quickly. – McGarnagle May 08 '12 at 05:56
  • i also think, @Html.HiddenFor is the standard way till now in MVC in such scenarios – Nirman Jan 07 '13 at 17:59
3

[ScaffoldColumn(false)], it simply hides the property. Suppose you have an id auto incremented property. The user don't have anything to do with this property. So use it to hide the property in View.

Md Shahriar
  • 2,072
  • 22
  • 11