MVC 3 and strongly-typed views - asp.net-mvc

I have been having multiple problems with getting the Add View.. “Create a strongly-typed view” dialog box option to work properly.
Most of the time, I am unable to get the Model classes to show up in the “Model Class” drop down. This last issue is with Entity Framework classes that have been generated within the Model folder (and namespace). I Rebuild the project and they still fail to show up.
In fact, if I add a test class to the Model folder with a few public properties—it is not showing up in the drop down.
Is their any information available on how this drop down pulls the Model classes to display to the end-user?
Regards,

Normally rebuilding the project should be enough to show your custom class. But unfortunately this is far from perfect. Happens to me also. What I do is that I create my view model (not entity framework model because you should be passing only view models to your views), copy the name into the clipboard and paste it in the Add View dialog. I find this faster than scrolling through a dropdown of 1000 classes and finding the correct one.

Related

Updating scaffolded controllers and views with model changes

For the basic functionality of my project I've been defining Entity Framework models and then using the scaffolding feature of Visual Studio to implement CRUD functionality (Right Click -> Add -> New Scaffolded Item...).
If I need to make a change to the model after I've done this, how do I make the scaffolded controllers and views update to the latest model changes? For example, I'm seeing the need now to add a new attribute to my model and change the data type of an existing one, but the controllers and views likely won't work after the change.
I could always just re-scaffold, but I've made some changes to the controller and views already which I don't want to lose. Is my only solution to make the changes myself?
Rename the files having your changes, scaffold again, and replace the newly-scaffolded files with your originals.
Naturally, you'll have to reconcile any domain changes that were made to the files containing your changes.

.net mvc how to render data after table row click

I’m new to .net mvc and have become stuck when rendering data in my view.
I’m creating a small project management tool and I am currently working on the project admin section.
I’m sending a model that contains a list of all projects that a user has to a view that administers projects. The project list is rendered in a table. The model contains everything relating to the projects, phases, project users etc.
When I click on a row in the table, I would like to render the extended detail of that project below that table so it will display users and phases of the selected project.
With jquery I can do this, but I would like to code this event in the view using .net code and not use Jquery. My main objective is not to call back to the server as I have all the data available in the model.
I’ve searched around and can’t find that much detail about triggering events and rendering further detail on a page. I understand what partials do, but that would mean calling back to the server.
What is the best way to approach this type of problem? Should I just use JQuery? If I do use jquery, how do I then use html helpers in the script, if the script is not in the page?
You could have a form inside each row and a submit button to show the detail. When the submit button is clicked a controller action will be invoked and set the selected row project id which will use it to retrieve the project detail and passed back to the view. You will also need to retrieve the whole list of projects because in order to be able to redisplay the table as well. Your main view model will consist of a property representing the collection of all projects and another property containing the selected project details. Initially the details property will be null. When a project is selected you will assign it. Then inside the view you could test if the details property is assigned and render the details in a partial.
If you decide to use jQuery and AJAX then since you won't be redisplaying the entire page, you don't need to retrieve the project list for the view model in the details action. Only the details. And then simply refresh the corresponding section of the DOM with the partial returned by the action.

What are pros and cons of adding a Model to MVC using EF VS project before and after a Controller and a View?

In tutorials and walkthroughs on developing an MVC3 (MVC2) using EF (EntityFramework, Entity Framework 4.1/4.2) I observe quite different orders of adding Model, View, Controller to a project in a Microsoft Visual Studio 2010.
What are cons and pros of different orders of adding M, V and Cs?
and of, for example, adding a model before and, more specifically, after a view and controller?
There is no specific rules for adding one over the other first. When you create an empty ASP.NET MVC3 Project, It will come with some default folder structure which includes one Controller folder, Views folder and Models folder.
Now If you are beginner, This is what i suggest. Add a controller first.
Simply right click ont he Controller folder and Select Add->Controller from the context menu and add your first controller(Give the name as HomeController). It will come with a Default Index action method and you can see a return View statement. Run your project now. It will show you an error saying that it can not find a view. So now it is the time to add a view. Go the index action in home controller. Right click on the Return View() statement and select Add View that will add a view (index.xshtml) under the home folder under Views. Now run the app and you will see the page content.
If you are going to interact with your database, you can add model classes. If you can add POCO class file to your Models folder or you can have it on a different library which is refered to this projcet. It's all up to you.
As Lavinski mentioned, If you create your models first, You can use Scaffolding to create the controller actions for you. But If you are beginner, I would suggest you to create your controllers and views by hand. That will help you to understand MVC configuration works

Database-first Add Controller not getting current model list

I've recently dropped and added a few tables from my database-first generated model (asp.net mvc4).
Now, when i try to create a controller (via context menu add controller), and try to choose the new "Client" model from the list of available models, the dropdown list is out of sync, showing some old models that had been dropped still, but not showing any of the newly added models. (screenshot below)
What can I do to get this list of models updated so that I can continue? I've tried restarting VS, but the problem persists.
Thanks!
You have to rebuild the project for any new classes to show up.

After updating to MVC3 RC, why don't my View Data classes show up in the Add View dialog

Background:
I just updated to the RC of MVC3, and when I went to add a Stongly-Typed View, the list of classes didn't contain the class I wanted, which has been built in my project.
Now, I did compile my project, and already have several views that work correct, but their model also doesn't show up in this list.
Since the change to this dialog in RC, what is the new criteria that makes a class show up, considering none of them in my project showed up.
They altered the way that they're displayed in MVC 3 RC. For example previously you would just scroll to:
Project.Data.Models.User
Now it's organized in reverse
User (Project.Data.Models)
This is most likely your problem. What would be nice is if they had a filter to show only local objects rather than all referenced assemblies
Had the same problem. Clean solution and build and my view models started showing up...

Resources