I am using MVC 5 controller with views,using entity framework and create, delete, details, edit, index cshtml pages occurs automatically. When I change the model classes and update the database CSHTML pages don't change. I am manually deleting view pages and recreating them with scaffolding. When I do these things my HTML designs are deleted.
How can I update aspnet-scaffolding cshtml pages automatically?
Note: I am using layout pages but they aren't solving my problem.
"When i do these things my html desings are deleted. "
Here is the root of the issue. If I understand you correctly you are:
Using scaffolding to generate Views.
Modifying the CSHTML of those views to customize the View.
Your hope is that after you modify the database, the scaffolding will update the views. However, when it generates the new CSHTML Views, how does it know how to combine the changes with your customizations? It doesn't. It can't know how to generate a completely new view and somehow integrate your customizations.
This is pretty much the same with any scenario that involves code generation. You have to make a choice. If you want to preserve your edits, you can no longer use the scaffolding to generate new views after editing. If you want to benefit from scaffolding after database updates, then you should never edit the CSHTML with the intention of customizing it.
I used scaffolding a long time ago only as a learning tool, to see how things like lists were handled. For probably 2 years I have never used scaffolding, and it is generally slow for larger projects, so I just always create an empty CSHTML or copy an existing one that is close to the approach I will take with the new one.
I would instead focus on other techniques that will help make creating CSHTML easier.
Using partial Views appropriately to pull reusable UI elements out.
Use Custom Object Templates to customize the results of things like DisplayFor and EditorFor.(I personally don't do this but I've seen others use it effectively)
Implemented helpers to take in parameters and produce HTML that fits the patterns you follow. Since I use bootstrap I created my own Html.LabelForBootstrap helper for example, that outputs HTML formatted for use with bootstrap with bootstrap classes.
Effective use of Layout pages and sections.
Eventually you will begin to see that the scaffolding isn't at all appropriate in many cases. E.g. the UI for relating one entity to another, the fact that it shows all columns when many aren't appropriate for display, etc.
You might be interested in Dynamic Data. It is more convention oriented in that it determines UI from database structure, but you provide customization conventions. Overall it's not really different conceptually from using MVC with Custom Object Templates:
http://msdn.microsoft.com/en-us/library/vstudio/ee845452(v=vs.100).aspx
Dynamic Data isn't mainstream, and skills you learn from using it probably aren't very marketable. It is probably only appropriate for administrative interfaces where user experience isn't a primary concern. Once you learn MVC really well you can pretty quickly spit out basic UI for new entities, so I don't recommend Dynamic Data.
It is possible to modify the default scaffolding templates. So if you wanted to, you could customize the template and have it generate your views. You will quickly run into the same issue however, as you will encounter a scenario where your generic template doesn't satisfy and you need to customize the output View CSHTML, but now you can no longer regenerate the View so the scaffolding template has limited usefulness:
http://www.hanselman.com/blog/ModifyingTheDefaultCodeGenerationscaffoldingTemplatesInASPNETMVC.aspx
Related
We have a situation where we need a read only version of an edit page. This is permission based as well as based on the status of the object. My thought is to separate this out into a completely new view but the other opinions are to place this logic in the edit view.
Some information about how the view is structured:
Form elements
Editor templates
Partial views
Kendo controls (fluent wrappers)
So in order to complete the task at hand means the read only functionality needs to exist in multiple places and must be passed to editor templates / partial views by view data.
There is also HTML helpers / extensions that are being used on the page which means updating these too.
Lastly, with the Kendo controls, a lot of the logic of the grid (in-line editing functionality) logic exists in external JS files so we have yet another place to modify the code.
My question is what is the 'best practice' in this regard?
Do we duplicate mark-up (cshtml) by separating the read only view from the edit view.
Or do we change the edit view to accommodate the read only functionality?
It just seems like changing the edit view will add a whole lot of complexity and dependencies for such a trivial task.
You can share the single view for readonly and editable elements by using razor conditioning in the view, But it seems to complicated to manage if the view content complex processing or elements, I recommend you to use separate view and send the appropriate view from the controller's action method depends on your criteria.
You can also use Html.EditorForModel directly to render elements as per your model data annotations OR Html.DisplayForModel to render all model properties for read only purpose.
Thanks in advance for your time :-)
I am working on an MVC3 application and a related database project in visual studio 2010 professional. I am switching between the projects as I revise database structure.
I'd like to know are there any tricks for updating the controllers and views in my mvc application? Updating the model (edmx and tt files) is a piece of cake. but it seems to be a huge chore to recreate the model and views without simply deleting them and starting over.
Deleting and starting over wouldn't be too bad if it didn't cause me to loose customization of the views and wreak havoc with my svn system...
Thanks again for any thoughts!
No, the code generation templates are meant to be run from the start, they are not intended to do change management of your controllers and views. If you have a standard template that you want to use, you can always use the T4 templates to create your own that will generate code that is closer to the end result you want.
But that is your only option, other than coding it by hand.
MVC Scaffolding would allow you to quickly re-generate views, controllers, repositories etc. However you would lose any customisations.You can customise the T4 templates to make it make pages more like you want. If your changes were generic that you always did then keeping them in the T4 template would mean they would also be regenerated by the scaffolding.
It is a good way to quickly add new functionality/or replace existing.
I am using ViewModels with asp.net MVC3. One of the thing I am curious about is, suppose I have an entity named Customers and it has Add, Edit, Delete screens. Assume that they all have different properties requirements.
For eg. Add may have address field but the edit screen may not have edit screen, delete may only use customer name more than anything else.
My question is, how do you create ViewModels for this? Do you go with the approach of shared ViewModels between add, edit and delete i.e a single viewmodel class that handles all for you or do you prefer to create viewmodels classes / page?
Advantage with shared viewmodel is it reduces development time and we can reuse classes. But big problem with this is that if you are using tool like Automapper you may expected results for different screens.
Disadvantage with one viewmodel/page is that it increases development time. Which way should I go?
My approach to view models is to use shared view models until the requirements (the data transported) to the view is different. That means I'm using a shared view model for example for CreateAddress and EditAddress in case all data transported to the view is the same. In case an additional field needs to be displayed in the view for example in the CreateAddress view I'm refactoring my view models and using different view models for CreateAddress and EditAddress.
For example for DeleteAddress I'd use a distinct view model from start because I know that the data displayed in the DeleteAddress view is almost never the same as in Create/EditAddress.
Another approach is to use dynamic view models, since view models should/must not implement business logic and act as DTOs between controller and view this approach has some benefits (No need to create logic free, throw away DTOs).
It depends upon situation. if you have similar requirements for different screens (validation, properties to render etc.) you can use the viewmodel across different views. If there is difference of one or two properties, i would still use the same viewmodel and where these properties are not needed i will put them in hidden inputs so they travel back with the form post not allowing unwanted results. Hidden fields, as all know, can be tweaked and its upon developer to decide if it is safe to use hidden fields. However, if i have different validation requirements for two screens then i definitely have to go with viewmodel/page approach. You can mix both approaches according to requirements as they say "there is no best way of doing things"
All fields that exists in a model can be changed. It doesn't matter if they are hidden or not. All the user has to do is to inspect your pages and try to figure out what fields exist in the model.
Then he can add those fields (for instance with Chrome Dev Tools) to make changes to them.
The safest way to get rid of that problem is to just have models that only have the fields that are allowed to be changed.
That said, go ahead and use the same model if all fields in the model should be allowed to be changed by all users. (And simply do not show the fields that should not be modified)
I have a couple of super simple databases. Basically they all consist of single tables that saves tracking/logging data etc.
Now I want to list this data but I'd like to find a solution that's applicable to all the different tables in all the databses. SO basically I'm looking for some way/some pattern of pointing a solution to a database, generate code and GUI and the publish the site. The tables can have huge amount of rows so I need functionally like paging etc but otherwise a simple list is all I'm looking for as a first step. I've been looking at Dynamic Data from MS - could this work? Other options? I'd really like the web sites to me ASP.NET MVC ...
As a seconds step I'd also like to have the possibility to change/add and delete data from the rows via the GUI.
Consider using ASP.NET MVC 2. It has extensive scaffolding capabilities that can auto-generate the views for you, yet allows a lot of fine tuning
Brad Wilson explains scaffolding here: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html
For MVC 2, see
http://aspnet.codeplex.com/
and
http://www.asp.net/mvc/download/
Paging is not included, but you can get that from MVCContrib, for example
You could roll your own scaffolding and generate the actual pages using T4 (http://www.olegsych.com/2007/12/how-to-create-a-simple-t4-template/ and others), or you could look into monorail (http://www.castleproject.org/monorail/index.html).
If you don't mind mandatory JavaScript then I would suggest jqGrid. It should be very simple to enumerate tables, columns, and generate jqGrid's columns from it (colModel). You can see an example of how I do a similar thing here - it does almost exactly what you need but for classes, not database. So, you can't use the solution "as is" because it's for domain classes, but it is very close, you can take it as a base and rewrite JqGridExtensions.JqGridModel to process db table definition, not domain class definition.
If your site has to work without JavaScript, then either MVC v2 with scaffolding will work, or you can try S#arp Architecture which also includes its own scaffolding out of the box, that generates full CRUD model/controller/views/repositories/etc. The benefit here is that you will still work with classes, not datasets. However as far as I understand you'll have to define scaffolding manually for each entity - but since scaffolding is very flexible, I'm sure you can write Uber-Scaffolding class that will enumerate your database and spit out sub-scaffolding files or just call them right away. A quote:
you programmatically create an object
called EntityScaffoldingDetails which
holds details such as the entity name,
the namespace, along with property
details such as the property name,
type, domain signature inclusion,
attributes, default test values, and
SQL generation details.
But, I better like the jqGrid way, because it's very easy to generate jqGrid model definition from the database schema which usually contains all the metadata. Of course, one can integrate S#arp scaffolding and jqGrid so that the first produces the latter. The benefit of scaffolding is that you have full control over the code, you can take it and tweak it. Because, you can't expect it to be 100% automatic - generator can't decide HasMany or ManyToMany, control type, hidden fields, etc. Of course you can use SQL metadata (properties maybe) to give hints for your scaffolding generator.
Now, if you need a ready out-of-the-box solution, I don't think there's one except for maybe commercial tools. The above will only allow you to assemble your own one. As for commercial tool, for example, Telerik Grid said to be able to "automatically infer its columns based on the properties of the data item it is bound to".
I was wondering if anyone knew of any way i can implement an application which will do the following.
Allow a user to specifiy a connection string to a sql db
Allow a user to specify a table in the db
Allow a user to specify columns from the specified table
Generate Views, a Controller with Crud methods, & Data access code on the fly for the specified table columns in a subdirectory on the current web app.
I'm aware that there are apps that currently do this (such as sharepoints list creation stuff), but i'd like to see how this was accomplished and recreate it for my own learning purposes.
Thanks alot for any help
Take a look at Microsoft's take on scaffolding, also, some time ago I was developing a taxonomy app and found this meta data model in codeproject
Edit: another cool SO link
Have you check out SharpArchitecture?
Anyway I fiddle with MVC 2 based AutoCrud when I'm not saving the world from aliens so I can give some pointers and point to things to check out:
Become familiar with how MVC 2 can auto scaffold up your edit screens
Understand that you'll have to pass "meta" information about your models somehow. In MVC 2, this is called ModelMetadata.
Tackle how to display related or associated models in aggregate root or parent screens
Learn how to generate code, and inspect ddl schema or meta information with T4 templates.
Thats all I can think off for now. This is not an easy task and a comprehensive answer is probably enough to fill a book.