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
Related
I have build an web application, and I want to delete some record in the database using ASP.NET MVC. But, in the delete Razor View, the CSS is not working, so the page is like this 1. This is the Delete Page of my application 2. I wonder if there are any solutions for this problem. Thank you in advance.
Check this document describe col-sm, and as i notice the CSS classes that you have been used were marked with red, so maybe the version of bootstrap doesn't contain those classes.
the reference:
https://www.w3schools.com/bootstrap/bootstrap_grid_examples.asp
I'm very newer developing .NET web applications. Before read, I'm so sorry if I'm not able to explain correctly or if I'm confusing about something of .NET. Hope all of you can give me some light in .NET environment.
Context: We have two different solutions because the original idea was to develop two different applications with no common pages or content between them. Now, we need to have one of the aspx page that is in the solution1 inside the solution2 (and probably more in the future) because it's common between them.The idea is not to have two different maintenance of the same page. This two solutions have only one project inside them and each project has his own master page.
Solution1:
-Project1
Solution2:
-Project2
Question 1: Is it possible to import or use complete aspx page across the solutions? I mean, I know that is possible to import aspx files from the project1 into the project2, but doing this way, every change into the original aspx file of project1 means that is needed a new import into project2 (or this is what I think) to have the page up to date. What is the best way to share content between different solutions?
Thanks in advance.
You should be able to add the aspx page which exists in one solution (and version controlled hopefully!), to your other solution by adding it as a linked file.
MSDN explains how Visual Studio allows you to add an item as a link rather than directly adding the file to your project.
By linking to a file, you can capture ongoing changes to a source file without having to manually update a copy whenever changes are made. However, if the underlying file is deleted, the link will be broken.
To create a link to an existing item
In Solution Explorer, select the target project.
On the Project menu, select Add Existing Item.
In the Add Existing Item dialog box, locate and select the project
item you want to link.
From the Open button drop-down list, select Add As Link.
https://msdn.microsoft.com/en-us/library/9f4t9t92(v=vs.90).aspx#Anchor_0
Another helpful article by Grant Winney on the topic here:
https://grantwinney.com/visual-studio-add-file-as-link/
How do you add MVC controls to umbraco?
I know in web forms you copy the aspx file into umbraco's usercontrols folder, and the control's dll file into umbraco's bin folder.
But i dont know how to do this for MVC controls.
In solution explorer Go to controller Folder and right click on it then add controller and write the code in it you want
I would recommend reading these to get a handle on how MVC works in Umbraco:
http://shazwazza.com/post/native-mvc-support-in-umbraco-coming-very-soon/
http://umbraco.com/follow-us/blog-archive/2012/10/30/getting-started-with-mvc-in-umbraco-410.aspx
I think you mean one of these two:
1. Child Actions
Read this: http://our.umbraco.org/documentation/reference/Mvc/child-actions
It works in the same way as child actions in Mvc, just that your controllers need to be SurfaceControllers
2. Macro Partials
If what you need is a simple re-usable user controls, then you only need to use partials. You can create a .cshtml file inside Views/MacroPartials then create login to /umbraco and create a Macro that references that partial.
You can the render it with #Umbraco.RenderMacro("myMacro")
I know how to customize a controller/view template for an MVC project. However, I have a few different MVC 3 Areas in my project and each Area has its own Models, Views and Controllers. Is there a way to specify different code templates for each Area? I tried to place a CodeTemplates folder in one of my Area sub folders but it didn't work.
The idea is, when I right click on any Controller folder in an Area and select Add -> Controller, I want it to use that Area controller template.
UPDATE:
I will be happy with being able to select the controller template I want to use, but still wondering if it is possible to specify CodeTemplates for the different areas.
Looks like MVC Scaffolding might be my only option here. I will show specific solution when I get it working.
Rereading you question, I realized my answer may not be what you are trying to do. Do you wan't the auto generated code in the view to use a different template? Or did you wan't the page to look differently, like with css changes, etc? My answer applies to the latter. If you want to use different code templates, it seems like (according to this) as long as you give them different names you should be able to select the one you wan't to use. You could also probably just remove the ones you don't want from the project then add them back later.
Create 2 files in the folder you want the template to be applied to, one named _ViewStart.cshtml and the other _Layout.cshtml
In the _ViewStart file, add this:
#{
Layout = "~/Views/Reports/_Layout.cshtml";
}
This will point to the _Layout so change the path as needed. In the _Layout file add whatever you want, just like the layout in the Shared directory. And for any other folder you want to use this layout, just add the _ViewStart file to that directory and point it to the _Layout file you want.
I tried the following:
I have a shared library (.dll) that contains these files:
Views
Search
PowerSearch.aspx
PowerSearch.aspx.cs
PowerSearch.aspx.designer.cs
The PowerSearch.aspx file contains my html code.
The PowerSearch.aspx.cs file contains this:
using System.Web.Mvc;
using CommonProject.Web.Shared.Controllers;
namespace CommonProject.Web.Shared.Views.Search
{
public partial class PowerSearch : ViewPage<SearchViewData>{}
}
And the designer I catually don't even care about cause it's not used anyways.
Nothing fancy, just a strongly typed view.
I basically pulled an existing, working view out of my asp.net mvc project, put it in a seperate library and changed the namespace to contain the word "Shared" as in "this will be shared amongst several mvc projects".
Then in my original asp.net mvc project I created the same structure, only now the aspx page contains nothing but the asp #Page rule.
The matching cs file contains:
using System.Web.Mvc;
using CommonProject.Web.Shared.Controllers;
namespace CommonProject.Web.DRE.Views.Search
{
public partial class PowerSearch : CommonProject.Web.Shared.Views.Search.PowerSearch { }
}
There are no compile errors and no run time exceptions either. There is only a huge blank page...
Anybody got an idea?
Make sure the assembly (dll) is in your bin folder that you are referencing. Also try adding the assembly namespace to your web.config otherwise your View will not be able to find the inherited page.
<pages>
<namespaces>
<add namespace="CommonProject.Web.Shared"/>
How much of a struggle was it to try to add an ASPX page to a class library?! It's not in the default list when you select 'Add New Item...'.
I know it's OK to store Controllers and Models in a separate library DLL/assembly, but I'm pretty sure it's not as trivial to store or share the Views. The problem is that the default view engine is looking in a specific folder on disk for the view (~/Views/Controller/ViewName.as[pc]x or ~/Views/Shared/ViewName.as[pc]x). With a library DLL, the compiler doesn't really have any idea of what it can do with your ASPX file. It's not code, so unless you have a 'Build Action' set, it's just going to ignore it. There are various 'Built Actions' but I think your only options are 'Copy' and 'Embed As Resource'. Copy isn't going to copy to the folder that you need it to (the Views folder in your ASP.NET MVC web project), although you could possibly write a build script or 'Custom Tool' that did that for you (with a bit of work).
Hammett (of Castle Monorail) fame (and now an MS Employee) came out with a sample that allows you to store Views inside library assemblies using a custom VirtualPathProvider class that is able to dig into the DLL and pull out the View (embedded as a resource). The sample application is just a concept right now, so you might hit some roadblocks, but it appears to work and looks like an exciting direction. You can find it on his blog here: MEF and ASP.NET MVC sample. Download the code and do some exploring.
This blog post ASP.NET MVC Plugins is not by the same author as the one above, but it gives another examination of the topic and points to another post here on StackOverflow where a similar question was asked: Using VirtualPathProvider to load ASP.NET MVC views from DLLs.
I've seen a post from Phil Haaaaaaaaaaaaaaaaaack on storing views in the database. It's using Ruby scripts instead of Web Forms, so I'm not 100% sure if you could adapt his sample to fit your needs or not. Check it out here: Scripting ASP.NET MVC Views Stored In The Database.
What does the source look like for the .aspx file? Make sure you are inheriting your view in the page directive like this:
<%# Page Title="" Inherits="CommonProject.Web.Shared.Views.Search.PowerSearch" %&>
Also, if you don't want to specify the whole namespace in your page directive, you can add this to the <namespaces /> section in your web.config:
<add namespace="CommonProject.Web.Shared.Views.Search"/>