ASP.NET MVC Add View with ViewModels in another project - c#

Well in my solution I have view models in a different project from the original MVC web application. There is a problem however, that when I click the view folder to add view, it will no longer be able to find the view models from dropdown list. Below is a screenshot that demonstrates what exactly the problem is:
Sure I have an alternative solution that I can just create an empty view and then manually write #model ViewModelProject.ViewModel on the top. This isnt an ideal workaround though as I end up losing the benefits of scaffolding in ASP.NET MVC. Is there a way to make the view models show if they are in a different project/assembly? Anyone knows how to achieve this?

Related

Can't use #model with #inherit in Umbraco 7

I've implemented Entity Framework into my project, since I need to insert some data via a form to a custom table in my SQL database.
I've scaffolded all the CRUD views and have a button that links to the create view. This all works well and fine, but I need the create view to use the Master template that contains my header, navigation and footer.
In my create view I need to import my model, but I am unable to both use the #model and the #inherit keywords in the same file, so I am unable to inherit the master template.
Is it not possible to use Entity Framework with Umbraco? Do I need to create a partial view or Macro?
If you want to use a master page you have to set it at the header of the page like this:
If the name of the master page is Master here is the code.
#{
Layout = "Master.cshtml";
}
If you are using a umbraco page and you want to pass some custom objects from your controller here is the code for that:
#inherits Umbraco.Web.Mvc.UmbracoViewPage<List<ViewModel>>
If you are using only custom logic from backend which is not related to umbraco dashboard itself you dont need a macro!
Remember to inherit from SurfaceController.
Here is another question which may tell you more in details how to create your model and pass it to the view.
Remember your model should iherit from 'RenderModel' and you have to use umbraco form to post data back to the server as well.
If you have anything else unclear just let me know!
You also may wish to consider the concept of a headless CMS. That way, your views, models or anything in your app won't be necessarily dependent on the classes of your CMS. There are numerous headless CMSes in the market. Although I work with one of the vendors, I really want you to choose to your own preferences.

Not getting the connection between the passing data from model and controller to view

i am deadly stuck at one place i tried scaffolding using various sites but now i am totally confused about the thing to do.
I have a project divided into there parts from them 2 are class libraries and one is main mvc project as shown here.
Now i added entity model in .Dal and i added references for each other as per needs. I tried to make a controller using scaffolding and it was success.
But the main problem is i cannot solve this error continuosly coming on running the project.
The error is this:
This is the method i am calling from controller as follows:
The view page:
I done this while adding controller with scaffolding:
The error clearly states that your view is expecting a different view Model & a list is being passed from controller. Crosscheck your code once & check the references for the view Model.
Actually i was wrong at view of the _Layout page.
The error was because of i added a hard-coded line in _Layout page as shown below that is why i was getting this error of not getting the respective model.
The error causing view is:
Now when i removed this line from _Layout page it was working as expected.
Thus i learned that the _Layout page is one masterpage of our application and remains consistent when used throughout the application
Thus the thing that cleared me is after view page is debugged the layout page is called at the end which is the base of all the views referring to it

Share PartialView between projects in a solution?

I am developing 2 MVC5 websites. They run seperately, but actually related to each other (1 Admin site, and 1 User site). In both site, I have to render a graphical board using HTML table. I am using PartialView with a Model to render it.
Currently, this is my solution structure:
Project.Common (A class library project, contain the model)
Project.Admin (MVC5 project for the Admin site), refering to Project.Common.
Project.User (MVC5 project for User site), refering to Project.Common.
To render the graphical board, I will use BoardData class from Project.Common, which is ok. But with current solution structure, I have to create 2 PartialView in each MVC5 project, which may be hard to maintain later.
Please suggest a good solution for reusing the PartialView. I already know I can write Helper, but that way, I have to write HTML code inside C# code, which is not very good. Is there any way to do this within Razor View?
One of the options you can use is add a link to a file. In this case keep your Partial view in just single project and in second project use Add -> Existing Item, select your partial view from the first project and click on a small down arrow next to the Add button and choose Add as Link. More info can be found on MSDN, see section Add an existing item as a link.
In this case you will have a partial view only in one project and any changes to it will automatically be applied to other project as well.
You should embed your views into the Project.Common dll, a detailed description can be found on https://visualstudiogallery.msdn.microsoft.com/f28290ce-d987-4f91-b034-707031e10ce6

Redirect from one Razor View to another Razor View

I am new to Razor Views and I am working on Xamarin studio, making a hybrid application that works only on WebView.
Long story short, I am not working with MVC. No models and no controllers here, only Razor Views. Now this might be a wrong question, but is it possible to navigate from one razor view to another??
Like, from one .cshtml to another .cshtml, without going through any Controller, because I don't have Controllers.
Any .NET helper like, #Html.ActionLink("Text", "someFolder/page2.cshtml");
Update 1
Screenshots of index.cshtml and MainActivity.cs, from which I am generating HTML for index:
The breakpoint is on the line that is generating the HTML.
No you cannot redirect to another view without calling action.
In asp.net mvc you have actions and against actions View is returned, we can show two views in a view without calling action but cannot redirect to some view without calling action.
If you don't need controllers then you are not following mvc architecture so then i would prefer to use simple html pages, there is no need to use mvc in this case.
you can use PartialView as shown below but it will just use one view in another view it does not redirect to another view as you have mentioned :-
#Html.Partial("~/YourPath", Model);
For more information :- http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC

C# MVC Debug which controller returned view

So I just started at a new company and I'm trying to understand their code and it is quite complex. I am trying to debug a page and I know which view it is and I can set a break point in the view but I can't figure out which controller returned this view. How can I do this?
Recap: I can break in a view and I need to figure out what controller it came from.
If the application is using the Razor view engine (.cshtml files) you can use the following inside your view/layout to display the controller and action names:
#ViewContext.RouteData.Values["controller"].ToString()
#ViewContext.RouteData.Values["action"].ToString()
If it is using the WebForms view engine (.aspx files) you can do something similar with:
<%= RouteData.Values["controller"]%>
<%= RouteData.Values["action"]%>
The controller class will usually be named as in the route data plus "Controller". The action names will usually match a method name in the controller.
You may also consider getting a branch of the project just for you, and then install glimpse via Nuget. That may help you understand better the application.
Views usually are named after Controller action names. And views are placed in folders named after controllers.
Check this one for folder structure: http://www.codeproject.com/Articles/492833/ASP-NET-MVC-4-Part-2-Project-Items
Also you can set breakpoints in controllers and see which one is hit.
And I recommend watching free pluralsight course on MVC. I helped me a lot when I started with MVC.

Categories

Resources