When using the "add view" dialog in vwd is there a common practice for presenting any primary key fields as readonly to the user in the view?
By default it appears the user could change the key field value (as it is presented in a textbox) and overwrite data for that record for which the key was accidentally changed to.
Do we have to store the original record's key and then make the key presented as a label and then on the update subtitute the original key value back in for the record?
Or is there something simpler?
If this post has helped you please mark as answer.
Alter the generated code to display the ID as a label or span instead of a textbox. Or remove the ID altogether from the generated code. With MVC 2, you can decorate the ID property of your object with [ScaffoldColumn(false)] to prevent it even appearing
in a template.
Just out of curiosity, If I choose to remove it completely from the generated code in the "Edit View", when the view is posted back where was the original key value stored at? Basically how does it associate which record to update then?
If this post has helped you please mark as answer.
It can't, unless you append it to the URL somewhere. It wasn't until after I suggested that that I noticed you were talking about Edit scenarios, so I added the hidden field idea.
aspfromatoz
Member
276 Points
98 Posts
Handling your primary key field when creating an Edit view (using add view dialog)
Apr 07, 2010 04:58 AM|LINK
When using the "add view" dialog in vwd is there a common practice for presenting any primary key fields as readonly to the user in the view?
By default it appears the user could change the key field value (as it is presented in a textbox) and overwrite data for that record for which the key was accidentally changed to.
Do we have to store the original record's key and then make the key presented as a label and then on the update subtitute the original key value back in for the record?
Or is there something simpler?
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Handling your primary key field when creating an Edit view (using add view dialog)
Apr 07, 2010 06:26 AM|LINK
Alter the generated code to display the ID as a label or span instead of a textbox. Or remove the ID altogether from the generated code. With MVC 2, you can decorate the ID property of your object with [ScaffoldColumn(false)] to prevent it even appearing in a template.
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html
You might also put it in a hidden field which means it's accessible on Postback, but not visible to the user.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
aspfromatoz
Member
276 Points
98 Posts
Re: Handling your primary key field when creating an Edit view (using add view dialog)
Apr 07, 2010 10:56 PM|LINK
Thanks I will try that.
Just out of curiosity, If I choose to remove it completely from the generated code in the "Edit View", when the view is posted back where was the original key value stored at? Basically how does it associate which record to update then?
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Handling your primary key field when creating an Edit view (using add view dialog)
Apr 08, 2010 06:06 AM|LINK
It can't, unless you append it to the URL somewhere. It wasn't until after I suggested that that I noticed you were talking about Edit scenarios, so I added the hidden field idea.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter