How to #HTML.ActionLink to a controller/view in another dll? - c#

I have an ASP.NET MVC solution which references to another class library, in which i have an MVC folder and inside of it "Controller", "Model" and "View" folders. I'd like to do something like
#Html.ActionLink("Text", "Index", "DllController")
which will link to the DllController/Index action in the dll and show a view from that lib. Debugging through this, I see that it goes into the Controller, but once it reaches
return View("MyView", model);
I receive the foolowing error:
The view 'MyView' or its master was not found or no view engine
supports the searched locations. The following locations were
searched: ~/Views/MyView/MyView.aspx
~/Views/MyView/MyView.ascx
~/Views/Shared/MyView.aspx
~/Views/Shared/MyView.ascx
~/Views/MyView/MyView.cshtml
~/Views/MyView/MyView.vbhtml
~/Views/Shared/MyView.cshtml
~/Views/Shared/MyView.vbhtml
~/Mvc/Views/MyView/MyView.cshtml
~/Mvc/Views/Shared/MyView.cshtml
I assume this is because the view is being searched for in the current assembly. Can i change this behavior to show my imported view correctly?

Related

_Viewstart with controller action

So my current (and I believe this is the default way) to use _ViewStart.cshtml is to do this:
Layout = "~/Views/_SomeSideMenu.cshtml";
Now my sidemenu is a bit complicated so it requires a controller. But I cannot figure out how to call a controller action for the ViewStart and let it return the PartialPage for it.
My current workaround involves using #Html.Action() in the _SomeSideMenu.cshtml file and fill TempData with the things I need.
Normally I would want to put that data in the ViewBag or Model, but these are not available since the Html.Action does not return the _SomeSideMenu.cshtml.
How would one go about calling the controller action to return the PartialPage that is found in the _ViewStart page?
Actually, you cannot do this in the ViewStart file. ViewStart is a file that can store common functionality related to all the views. These pages can also be nested, so if you have multiple controllers, each controller’s views can have their own ViewStart page.
You should add your sidemenu into a Layout page. You could add some conditions to have this output into layout page. For sample, jere, we have an action filter which add a key into ViewBag and we use this key to have a state for a sidemenu (state like open or close and position in left or right).
You also could change your default layout page in your view just doing what the ViewStart page does, chaging the Layout property.
Layout = "~/Views/Shared/CustomLayoutPage.cshtml";

MVC 4 Intranet application, multiple layout

I am getting an error in developing a MVC4 Intranet application, trying to use multiple page layouts. As I see, the problem is not present when Internet application is used, here is quick demo.
Create new Internet application using Razor engine. Copy file _Layout.cshtml to Popup_Layout.cshtml (both in ~/Views/Shared), and strip header and footer sections in Popup_Layout. In ~/Controllers/HomeController.cs modify Index action with following code:
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
string layout = "_Layout";
if (DateTime.Now.Ticks % 2 == 1)
{
layout = "Popup_Layout";
}
return View("Index", layout);
}
That creates a simple game which randomly chooses to use one of two available page layouts. When compiled and tested, with refresh in browser we can see start page with or without header and footer sections.
Now if we repeat the exactly same scenario with only difference that project is Intranet application - build goes OK, but with refreshes in browser instead of getting a page without header and footer we get the server error, with the top of Stack trace says:
[InvalidOperationException: The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Popup_Layout.master
~/Views/Shared/Popup_Layout.master
~/Views/Home/Popup_Layout.cshtml
~/Views/Home/Popup_Layout.vbhtml
~/Views/Shared/Popup_Layout.cshtml
~/Views/Shared/Popup_Layout.vbhtml]
System.Web.Mvc.ViewResult.FindView(ControllerContext context) +399386
As an alternative, layout can be determined in View itself with assignment ~/Views/Shared/Popup_Layout.cshtml to Layout property, but it also throws an error.
I didn't find the solution using web, so I hope someone can help me here. Thanks
Try deleting Popup_Layout.cshtml then instead of copy pasting the _Layout.cshtml create a new empty view page for Popup_Layout. After you have a new empty view page copy paste the content (if you wish). I don't know why but I tried it on my test project and it worked. I used a regular web app (not an intranet app) but I experienced the same missing view error you described.
I think the key is the way the view file was created. I copied the file and renamed it and seen the same error. When I properly created a new view and then copied in the content it all worked.
Dear noviKorisnik i tried to replicate your error as per you written here but i am not getting the error as you specified .
Your error first line is
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
I believe mistakenly you deleted the index.cshtml .
Please be sure you have index.cshtml in the right folder .if you still get the error please give more detail .

Priority of razor views over the aspx in mixed MVC3 project

I've converted old MVC2 project to MVC3. Now I have .aspx views along with razor .cshtml.
Let's say I have a view associated with controller (HomeController, Index action and ~\Views\Home\Index.aspx) and at the same time I still have absolutely different ~\Views\Shared\Index.aspx.
Normally when Index Action calls View() it renders ~\Views\Home\Index.aspx. But if I convert the view into razor view, the same action instead of rendering ~\Views\Home\Index.cshtml calls
~\Views\Shared\Index.aspx.
So I guess MVC gives priority to .aspx pages rather to .cshtml. Maybe I need to change something in web.config files, because now I have to explicitly tell it which view to get:
View("~\Views\Home\Index.cshtml")
Even if I drop the extension View("~\Views\Home\Index") it will still call the shared .aspx view, although I have the right path. Strange isn't it?
The problem you are having is the default priority of MVC views. Here is the default:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
As you can see, it favors all aspx/ascx files, even in the shared directory, before similar razor views.
One solution is to Get MVC Razor favor .cshtml over .aspx, however this solution does NOT reassociated the aspx/ascx files. So you'd need something like:
protected void Application_Start()
{
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
ViewEngines.Engines.Add(new WebFormViewEngine());
}

asp.net mvc resource cannot be found controller

i got stack on my project the problem is after adding new controller class and views for the actions unable to link the views with existing views
*
The resource cannot be found
.*
i have home and account controller classes which work well and i add new controller class users and i have to action methods index and adduser both have view but anytime when i try to retrieve view from the new controller class it dosnt found the pages.
eg <%= Html.ActionLink("users", "Index", "users")%> please help !!!
have you created views?
Guideline:
create controller controller_nameController
create action public actionresult action_name(){return View();}
create views Hit ctrl+m, ctrl+v or right click on the action whose view you would like to create.
4. check if you really have created a view named action_name in controller_name folder
Then you can create links: ("to display text", "action name", "controller name")
Hope this helps.

Error using Model reference in strong-typed View in an MVC project

I am trying to create a new strong-typed view for an MVC project.
I tried to create it both from the Controller class (right click -> add View) or directly from the Views (right click -> Add view) and selected it to be a strong-typed view. From the drop-down, I selected the Model and data class it should refer to.
While filling in the content of my View, I need to declare it as:
<p>
Title:
<%= Html.Encode(Model.Title) %>
</p>
For some reason, I just get this error:
"The name 'Model' does not exist in the current context".
I must be missing something out... :-(
Any comment or idea would be great!
Does your code behind class inherit from ViewPage<ModelType>?
It should be <%= Html.Encode(ViewData.Model.Title) %>
I had a similar problem when adding MVC Views to an existing WebForms application. I resolved it by adding a Web.config file to the Views folder, copying the contents from another MVC project.

Categories