where is my "Create strongly-typed view" menu option in MVC 5 Visual Studio 2013 - asp.net-mvc

I am starting to create MVC 5 applications. But in controller, right click inside Create action method, select Create View. Menu comes up, where I can select model, etc., but there is no longer an option to select "Create strongly-typed view". See screenshot below

Hi MVC3/ or Visual Studio 2015 does not give option to select 'Strongly typed view', instead following steps need to carry out to implement strongly typed views.
Build your project before starting following steps so that you will get model in the list to select as strongly type.
Right click on the controller to generate view.
By default you will see following screen
Change options from Template : Empty (without model) to 'Empty' like below
This will allow you to choose your 'Strongly typed' View.
When View is created it will look like following
Let me know in case of any queries.

VS2013 changed the language a bit. To create "strongly typed" view, trigger the create a view dialog and choose any template other than "Empty (without model)". You should now be able to choose your Model from the "Model Class" dropdown. Doing this will create the equivalent of a strongly typed view.

Related

MVC4 - after adding new controller, corresponding view is not generated

I started learning Asp.NET MVC and got stuck pretty immediately. According to all materials I have, after I add controller in MVC project (Template - empty MVC controller), corresponding view should be created, too (folder under Views).
However, when I do this, nothing happens. Does anybody know what could be the problem?
And will this cause me a problems in a long run? I guess I could create those files manually, but still would prefer if they were generated...
My system:
Visual Studio Professional 2013, Update 5
Project: new MVC Web Application, template "Internet Application"
Thank you
Other possible way is to add complete path of the view in your controller method which is returning a view.
public ActionResult Index()
{
return View("~/Views/Home/Index.cshtml");
}
this method is useful when you have a hierarchy of folders for view files.
Normally views aren't created when a new controller is added. Views can be generated with the following methods:
Simply right click the method name within any controller and select "Add View"
Right click on the controllers folder and select "Scaffold..." which will created a new controller, views and/or DataContext for a specified model.

create the cshtml file based on user defined controls (like automatic code generator)

We have input like the control type and the name (for eg : Control Type : Label, dropdowm, radio button , etc., and Control Name : lblID) On click of a Generate, I need a CShtml page which should be binded with all the controls. Is it possible to integrate T4 template with my solution. We are yet to start a new application.
Heard about T4 template but all the examples specified with scaffolding options. I don't have this. By generally i need to create cshtml under views of my project. Requesting suggestion.
suggest a way to proceed. Thanks in advance.

Can't select View Content dropdown when adding view in MVC using Interfaces

I have my Model defined externally in two projects - a Core project and an Interface project.
I am opening the Add View dialogue from my controller, and selecting Create a strongly typed view.
In the drop down list, I can select the concrete types like MyProject.Model.Core.OrderDetails, but the interface types like MyProject.Model.Interface.IOrderDetails aren't there.
I can type the interface class in manually and everything works, but then the View content menu that lets you select the Create, Delete, List, etc scaffolding is disabled.
Is there some problem with using interfaces in MVC? Or is it something else I'm missing?
Edit: Just to clarify, if I select the concrete object and the whatever scaffolding I want, I can then edit the Inherits tag in the view Page directive and everything works fine, so there's no missing references or anything. It's just the wizard doesn't seem to want to work with the interface.
Well, you could always select the concrete class implementing the interface, generate the partial view, and manually remove all the stuff that's not needed.

Providing data to Menu in my ASP.NET MVC Master Page

We are beginning the process of moving from Web Forms to MVC for all of our new applications. I am working on porting our Master Page over and am trying to satisfy the requirements that we need a single master page to be used by all applications. The primary navigation for the application needs to be in a menu within the master page. Accomplishing this was easy, the hard part is that each application may need to determine what to display in the menu using a unique set of rules. Some apps can simply say, here's the menu structure to use via something like a SiteMap. Others need to determine what is displayed in the menu based on what roles the user has, this can also be handled easily with a SiteMap. The situation that I'm struggling with is that some apps need to generate the menus based on the roles the user has, but also on the data on which they are working. i.e. The same user may have different option in the menu for a page if they are working on object 'foo' than they do if working on object 'bar'.
What I've done at this point, is I've created an HtmlHelper that is called by the master page view and takes a list of objects of a custom type and returns an unordered list that is styled by a jQuery plugin to display the menu. The list of objects the helper method takes are passed to the view using the ViewData dictionary. Currently, the value of this ViewData node is set within the constructor of each controller. This allows each page, and potentially each method, to set a different menu without having to set the value in each action method, unless its needed. I have also created a class that parses a SiteMap and returns the list of items needed to build the menu. This class is what I'm using to set the ViewData value in the controller. The idea being that if an application needed more control of how the menu data was generated, they could create their own class to generate the data as long as it returns a list of the correct type of objects.
This solution seems to work fine so far, it just doesn't 'feel' right for some reason. I'm hoping that I can either get some ideas of better way to do this or some reassurance that this is a valid approach to solving this problem.
If it is something that will be on every page, do something like this:
Create a base controller:
public class MyBaseController : Controller
Have this controller get the data it needs and send that data in the ViewData["menu"] to the View. Then have all your controllers inherit from this one:
public class HomeController : MyBaseController
In the Master Page, loop through your ViewData and create your menu.
(I did something like this for my sub-menu which displayed a list of categories.)
In the book I am reading (Pro ASP.NET MVC Framework by Apress) they use Html.RenderAction for the menu in the masterpage. I am a Asp.net MVC novice so maybe somebody else can give more info about this.
You can download the sourcecode at apress.com though so maybe that could help.

How does ASP.NET MVC link views and controllers?

What code does Visual Studio add (and where is it put?) when you right-click the controller method to link to the view?
How can one do this (link the controller & view) without Visual Studio?
It is all by convention.
You place your views in the Views/ControllerName folder for every controller and that's the default location for framework to look for. But it is not a must in any way.
When in your controller you write
return View();
Framework assumes you want the view with the same name as action name and looks for it in Views/Controller/ folder. Then Views/Shared.
But in your actions you can write
return View("ViewName");
Framework will look for a View named "ViewName" then in same folders.
So default name for a view would be the name of action being executed. And that's a convention.
By default asp.net MVC uses FormViewEngine, which is an implementation of IViewEngine. IViewEngine has got two methods called "FindView" and "FindPartialView" which actually locates the view file from "Views/Controller/" folder.
Thanks,
Rajeesh
Visual Studio will create a Folder (if it doesn't already exist) under ~/Views/{YourControllerName} and put your view in there. If it doesn't find it in there it will look in the ~/Views/Shared folder. If you want to manually create a view you need to add your page to one of those folders, preferably the ~/Views/{YourControllerName} folder. Hit up the NerdDinner tutorial to see this in action.
http://nerddinnerbook.s3.amazonaws.com/Intro.htm
Visual Studio uses templates to create the default views. The templates are located in the [Visual Studio Install Directory]\Common7\IDE\ItemTemplates[CSharp|VisualBasic]\Web\MVC\CodeTemplates folder.
If you wish to create an MVC .ASPX page manually, you need to simply create a blank page and provide a Page directive with the following attributes:
Language ("C#" or "VB")
MasterPageFile (default is ~/Views/Shared/Site.Master)
Inherits (for strongly-typed models, use ViewPage<ModelClassName>; otherwise ViewPage)
Example:
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="ViewPage<ListCompanyManagerDetailsViewModel>" %>
For user controls (.ASCX), the same rules apply, except the MasterPageFile attribute is not used and you inherit from ViewUserControl.
Example:
<%# Control Language="C#" Inherits="ViewUserControl<Contact>" %>
P.S. The reason that namespaces do not precede any of my class names is because I declared them in the section of my web.config.

Resources