My MVC project/ view/ controller not seeing my models in Umbraco app - asp.net-mvc

I'm building an Umbraco 7.2.8 website
Under installation Models or Controllers folders didn't exist so I added them.
I added a model class to Models folder:
namespace MyNamespace.Models
{
public class QuoteRequest
{
public int People { get; set; }
public int Days { get; set; }
}
}
When I'm trying to use it in a view I get error The type or namespace name 'MyNamespace' could not be found (are you missing a using directive or an assembly reference?)
#using MyNamespace.Models
My controller doesn't see it either. Why is that? What do I miss?
I tried to compile the project but with no effect. The project is compiling but no MyNamespace.dll is placed in bin folder.

Did you crate those folders in a separate project. If it's the case got to "Add references" and select your other project.
If it's in the same project you need to use the same namespace as your project name/namespace.
But I advise you to create a new project, it's a cleaner architecture.
You could also add your code into the App_code folder. The code will be available anywhere in your project.
Hope it helps.

Related

How do I add an Image/media field in Sitefinity?

I am using mvc, and I have created a controller for the custom widget which will allow the user to choose the image that they watn displayed. How do I specify this field?
I have:
public class Test: Controller
{
public ImageSelector Image { get; set; }
}
You need the so called custom designer for your MVC widget.
Check this article for all the steps/details you need:
https://www.progress.com/documentation/sitefinity-cms/image-selector-mvc
UPDATE: Here is Github Gist with working code:
https://gist.github.com/VesselinVassilev/31b159a1985f272e85b4ff167e59c8d8
Note: the DemoController.cs file should be in the /mvc/controllers folder
the rest of the files should go to the /mvc/views/demo/ folder.

Step by step process of using resource.resx files(created outside the project using class library) in asp.net mvc4

I am creating an application using mvc4. In my application i created the resource.resx file outside the project (but in one solution where my mvc application is also reside) in class library. I want to use the label name or validation message from resource.resx file.
Like:-
public class xyz
{
[Display(Name = "LBL_name", ResourceType = typeof(Resource.abcd.Resource1))]
public string Name1 { get; set; }
}
"LBL_name" is specified in Resource1.resx file. "Resource.abcd" is its custom tool namespace.
I added the reference of class library to my mvc application and set the properties of resource.resx as suggested in this link.
But it gives the following error
Cannot retrieve property 'Name' because localization failed.
Type 'Resource.abcd.Resource1' is not public or does not contain a
public static string property with the name 'LBL_name'.
I am not sure of using the resource.resx file correctly. Can someone guide me one this.
Open the file in the resource editor in VS and make sure the access modifier param is set to Public.

Should I use a partial class for Entity Framework DBContext in another namespace

I have two projects in my solution:
Project 1: Main web project (namespace: MVCWebsite) [big]
Project 2: Daemon project (for scheduled tasks) (namespace: MVCDaemon) [small] [justcodingnow]
Project 1 has a EF DBContext, Project 2 will have EF collection (in the same context, as it uses Project 1's collections).
What is the best way to structure this?
Should I have a class in Project 2, with the same namespace as Project 1, and use a partial class of the Project 1's DBContext.
E.G, Project 1:
namespace MVCWebsite.Models
{
public partial class BaseDBContext : DbContext
{
}
}
& Project 2:
namespace MVCWebsite.Models
{
public partial class BaseDBContext : DbContext
{
}
}
I.E It is definitely unintinutive to have a namespace.class file in a project, whereby the normal namespace is something else!!
Is this a terrible/dangerous thing to do? Is there a better way to architect this?
All partial class code files must reside within the same assembly.
Hope that makes the decision easier.
The better solution is to create a 3rd project that contains the full definition of DBContext class with all the collection that is referenced from both Daemon project and web project.

Share variable in web.config Across Projects in a Solution in Visual Studio

I have one solution that contains 3 projects : ASP.net MVC 4.0, Share Library and Web Service project. I want to share some of key value in appSettings of Web.Config in ASP.net mvc Project to Web Service Project. I found this answer but it's now working for me.
Could anyone give me some idea please.
Thanks you so much.
You could store that information into a dll that you would share accross all your projects :
Create a new Class Library project :
Name your class MyConfiguration.cs for example
namespace CentralizedInformation
{
public static class MyConfiguration
{
public static string Value1 = "Test string value 1";
public static int Value2 = 2;
}
}
After you can add the reference in all your projects (Project -> add reference -> Browse -> Browse to CentralizedInformation.dll)
and add itin your projects (here an mvc project example)
using CentralizedInformation;
//for asp net mvc project
protected void Application_Start()
{
string mySharedAccrossAllProjectsString = CentralizedInformation.MyConfiguration.Value1;
//put it wherever you need it session or anything else
}
Juste reference the same in your other projects and you are done. Also rebuild your projects when you want to update the data ...
That dll could have a config file and you could programmatically expose all it's data.
I admit it's a quite funny and complicated way to do such a simple thing. But that's a way ^^

How to build ASP.NET MVC user controls into class libraries

I'm trying to build some .ascx controls into a class library for plugins for a CMS I'm building.
My project type is a typical C# class libary with references added for system.web.mvc & friends.
My problem arises in trying to create a strongly-typed user control. My control looks like this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TagCloudWidget.Models.TagCloudWidgetData>" %>
Even though I have another public class in the same project under the namespace TagCloudWidget.Models, I can't seem to get the .ascx file to recognize this. I've tried including Imports for the namespace, but then it just complains that the namespace doesn't exist. The TagCloudData class file looks like this:
namespace TagCloudWidget.Models
{
public class TagCloudWidgetData
{
public IList<TagCount> TagCounts { get; set; }
public ContentTypes ContentType;
public string Controller;
public string Action;
public TagCloudWidgetData(IList<TagCount> tagCounts, ContentTypes contentType, string controller, string action)
{
TagCounts = tagCounts;
ContentType = contentType;
Controller = controller;
Action = action;
}
}
}
Can you see what I'm doing wrong?
Since the suggestion below, I've tried adding a code-behind file. I think I'm closer to figuring out what's wrong, but I'm still not able to find a solution. In the project, I've added references to System.Web and System.Web.Mvc. But, in the using section of the codebehind file, when I type "using System.", auto-complete doesn't even show System.Web or System.Web.Mvc as available options. It's like they just don't exist on disc. Other projects are able to include them and reference them just fine. I suppose this is the source of my problem, so I'm hopeful once I figure this out the rest will just fall into place. Ideas?
I've had similar problems with an application that I started with the beta release. The only thing I've found that works is to create a code-behind file for the control and define the inheritance in the code-behind. Then in the control, have it inherit from the class defined in your code-behind. In newer projects, built from scratch using 1.0, I don't have this problem -- but then again I don't have my partial classes defined in a library, either.
Success! Somehow, my .ascx file had been marked "content" in the file properties instead of "compile". Once I fixed that, I can now reference the necessary assemblies and load/run the plugin without problems. Thanks for the tip on the codebehind file!
(update) The other tip I figured out last night is that my plugin also has to be in the app's /bin directory as well as my /widgets directory, otherwise I get a "Could not load type..." exception. I haven't figured out yet how to wire up ASP.NET MVC so I only have to have it in 1 place, but when I do, I'll post back here.

Resources