asp.net-mvc 在 MVC 3 模型 ID 属性中将 ScaffoldColumn 属性设置为 False 的真正优势是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10493066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the Real Advantage to ScaffoldColumn Attribute set to False in MVC 3 Model ID Properties
提问by Shawn
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 ID's 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.
我是 MVC 的新手,当我为代码优先方法构建模型类时,每个键 ID 列都[ScaffoldColumn(false)]设置了属性。我这样做是因为我不希望 ID 明显显示在视图中,而且我知道解决此问题的方法是删除 UI 在每个包含 ID 字段的视图中生成的 HTML。
The problem I ran into by using ScaffoldColumnset to falsein 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 HttpPostmethod.
我在模型中使用ScaffoldColumnset to遇到的问题false是在触发视图的 Edit 方法时第一次注意到的,我收到了这个错误:存储更新、插入或删除语句影响了意外的行数 (0)。当我在调试中运行代码时,ID 属性实际上设置为零,如我的视图 EditHttpPost方法中所示。
My Question that I hope someone might elaborate on, is what's the point in having an attribute like ScaffoldColumnin 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?
我希望有人能详细说明我的问题,ScaffoldColumn如果 ID 没有被发送到控制器方法(例如在这种情况下是 Edit ),那么在 MVC中拥有一个属性有什么意义HttpPost?我是否应该以旧方式解决这个问题,并删除由未将此属性添加到键/外键属性时生成的 MVC 寺庙创建的每个视图中的 HTML 标记?
The work around I found if you set this attribute to falsewas adding an ID param to each HttpPostmethod, and than setting the appropriate ID property field prior to calling SaveChanges().
如果将此属性设置为,我发现的解决方法false是向每个HttpPost方法添加一个 ID 参数,而不是在调用SaveChanges().
采纳答案by McGarnagle
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)
回答by Simply Me
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.
这样,您的字段将使用视图中的隐藏输入显示。无需进行任何更改,视图将知道如何显示它。
回答by Md Shahriar
[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.
[ScaffoldColumn(false)],它只是隐藏了属性。假设您有一个 id 自动递增属性。用户与此属性没有任何关系。所以用它来隐藏View中的属性。

