Scaffold View with ViewModel in ASP.NET Core 5 - visual-studio-2019

I am trying to scaffold views based on a ViewModel in ASP.NET Core 5 project and have been finding it difficult passing through the following stage
I used to be able to pass this stage by deleting the Data Context field but that seems to have become impossible recently. Any ideas on how to scaffold a razor view with a ViewModel on ASP.Net Core 5?
With the DataContext field, it generates an error as shown
The model doesnt need a key. It is a ViewModel. It does not even need to be in the AppDbContext file and it does not need to be be in the database for any reason

Related

MVC data design decision

I am currently in the beginning of designing an MVC application.
While running some tests through a prototype I came across an issue with the way some data is being handled.
Here's a simple DB diagram
When working on my prototype, I noticed that when the Entity Framework builds all of the required models from SQL it creates an ICollection relationship from the Employee_Table to the Job_Position_Link_Table. I cannot access attributes directly in the Job_Position_Link_Table if the view is strongly typed to the Employee_Table. Also I cannot get to the Job_Position_Title_Table as well. I looked into creating a ViewModel for this issue, but cannot find a good tutorial on how to create the ViewModel when you are working with an ICollection. Most tutorials are from a code first approach, and I am dealing with an inherited database.
The other solution I was thinking of was creating all of my Views in SQL and then passing them into MVC.
I am still new to MVC, and was just wondering what the best practice in this scenario would be?
You should never bind your views to EF entities. Instead, create view model classes with required properties and convert the EF entities into these VMs (you can do that manually or with the help of auto mapper). Then, your views can be bound to your view models.
Its the job of controller to build the view model and send it to view engine for rendering. Thus, view must expect a view model ready for presentation.

Creating model from ado.net code

Is there a way to create a model from an ado.net SqlDataReader? I would really prefer to use Asp.net MVC 5 coming from Ruby on Rails rather than Webforms, but I need a way to display the data from an existing database on the view. Or could I possibly do this without creating a model by handling this in the controller? I don't want to create a model based on column names in the table in case the table (or db schema) changes later on.
Use Entity Framework`s DbContext.
Looking around, the normal solution would be to reverse-engineer the model from the database found here: http://www.codeproject.com/Articles/671590/Reverse-Engineering-an-Existing-Database-in-your-A
Unfortunately, this is something that usually has to be done through Visual Studio, which I will not always have access to once the application is in production mode. If the schema changes, I would not be able to use the Update Model from Database... command in VS 2013. Therefore, I'll have to use Asp.net Web Forms for my Database-First application.

Generating view with scaffolding for ASP.NET MVC3

I'm doing a project with ASP.NET MVC3 and Linq to Entity . I have separated my data access layer through a different project and of course its not highly coupled with Model.
I have seen the scaffolding ( auto code generation for controller and corresponding views) feature which depends on Model.
Is there any tools or specific procedure through which I will be able to generate views for a specific controller like scaffolding do for ASP.NET MVC without involving model highly like MVC do ?
Thanks
You can go through the MVC Scaffolding articles. Basically what you have to do is edit the default templates and power shell scripts.
The model that you are talking about here doesn't have to be a Database generated class. It can be anything, so what I would do is create a ViewModel (class) that will represent that view you want to display and use the Scaffolding functionality on that ViewModel.
You can then use something like an Automapper to bind your models to the view models

Retrieving form values from ASP.NET MVC application

I am learning ASP.NET MVC, and ran across a video on the asp.net/mvc website that showed how to retrieve a value from a textbox after a postback. In the video the author simply grabs the value from the Request object in the controller.
It seems like this breaks the separation of concerns concept? By doing this the controller is now dependent upon the presence of a Request object which won't exist if one runs unit tests against the controller.
So I assume this is an incorrect way of retrieving form data on a postback. What is the correct way? Once I am in my controller, how do I get access to the postback data?
It seems there should be some intermediate step that essentially pulls the data from the postback and packages it into a nice object or some other format that the controller would then used?
The data should be posted back to your Model or ViewModel. Your controller method that handles the POST will expect the model to be provided as a parameter.
Here is a blog entry that gives an example
Using model binding, MVC can populate data coming from the form data, the query sting, cookies, and a number of other sources directly into your object model or other paramters defined as parameters to your action methods in the controller.
There's too many details of how this works to summarize here, but it is the cornerstone of the power of ASP.NET MVC.
Check out Models and Validation in ASP.NET MVC as a good starting point. You'll find tons of other resources around MVC model binding out there.
I've really liked Steven Sanderson's Pro ASP.NET MVC 2 Framework if you prefer physical books.

ASP.NET MVC inserting form values into a ViewModel

I'm trying to work out if there's a built in way in ASP.NET MVC to assign the form values that are POST'd back to the properties of the ViewModel that was originally sent to the View?
So I'm thinking along the ideas of decorating some of the properties in the ViewModel with an attribute and then reflecting over the ViewModel and using that name to extract values (and coerce) from the Form[] object.
However, I'd imagine that something like this was already built in and so don't want to re-invent the wheel here.
The problem that I'm trying to solve is that a user clicks a button on a form and the server validates the data and if there are errors we return the user to the form by using the same ViewModel to carry the data and thereby fill the values back into the form that the user originally entered.
(Yes, I'm also doing client side validation using JavaScript to make this lightweight but for security I have to repeat validation on the server.)
Ideas?
You can use UpdateModel or TryUpdateModel in your controller.
I recommend using one of the overloads in which you specify the fields to be updated.
This is discussed in detail on page 78 of the Wrox Professional ASP.net ebook (or echapter!)
I don't think MVC has anything like this built in, or at least I haven't seen anything. It would be nice though as many other MVC frameworks do this (struts for example).

Resources