Solved

ASP.Net MVC 4 C# -- scaffolding template, last modified date ?

Posted on 2013-06-04
5
1,428 Views
Last Modified: 2013-06-20
How can I change below #8 to automatically populate the POdetail database columns (attached) ?

I don't want to hand code CreatedBy, LastModifiedBy, CreateDate, ModifiedDate each time I add a new table via the below like http://stackoverflow.com/questions/10935803/successful-model-editing-without-a-bunch-of-hidden-fields talks about.
----------------------------------------------------------------------------------
Steps
 1. create new APP
 2. RMB on Models FOLDER, Add, ADO.NET Entity Data Model
      ** Server = DBTEST
      ** Database = MVC_test (make sure table has "PK=ID" so Edit, Details, Delete work
 3. REBUILD
 4. RMB on Controllers FOLDER, Add, Controller, Default1Controller
      ** Template = MVC with read/write actions and views, using Entity Framework
      ** Model Class = Table6 (MvcApplication14.Models)
      ** Data Context Class = MVC_testEntities (MvcApplication14.Models)
 5. edit /Views/Shared/_Layout.cshtml --> <li>@Html.ActionLink("Test Text", "Index", "Default1")</li>
 6. change /Filters/InitializeSimpleMembershipAttribute.cs & Web.config to use ASP.net MVC 4 "SimpleMembership"
 7. run APP, login, click "Test Text", click "Create New", save
 8. WORKS, but displays CreatedBy, LastModifiedBy, CreateDate, ModifiedDate as editable textboxes and does not enter default values
      ** CreatedBy, LastModifiedBy, CreateDate, ModifiedDate need to automatically populate as HIDDEN fields
0
Comment
Question by:finance_teacher
  • 3
  • 2
5 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
Comment Utility
This is probably a good case of where you should use a viewmodel.  The viewmodel class will have the needed fields, but not the createdby, lastmodified, etc.  Thus you will avoid sending them to the client at all.

While you could use the domain class as the model and just omit the "invisible" fields from the view, you would be vulnerable to binding attacks, where anyone could inject whatever value they wanted into the supposedly invisible fields.

While you might not want to hand edit fields, views, etc. you are going to find out that it is generally a necessity.  The scaffolding is rudimentary at best, unless you plan to write your own scaffolding code.
0
 

Author Comment

by:finance_teacher
Comment Utility
Can you provide some URL examples of the createdby, lastmodified, etc ?
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
I'm not really clear on what you need.
0
 

Author Comment

by:finance_teacher
Comment Utility
Just some website examples showing a examples of the createdby, lastmodified, etc in ViewModel/etc like you talked about.
0
 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
The point is that createdby, lastmodified, is not in the viewmodel, but in the Entity Framework model.  Any example of viewmodel will illustrate this concept.  This was covered in:

http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/

The scaffolding then will not include those fields for the view.  You still have to write code to handle the fields when you do the database work, but that's not really avoidable.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now