MVC view can't see model in separate project - asp.net-mvc-3

I have an MVC view in one project (my web project). I'm trying to specify that it should inherit from a class (the model) in another project, but that class isn't visible to my view. The model class's project is in the References of the project in which the view resides. Why can't my view see the model class? Here's the view in its entirety:
<%# Page Title="Title"
Language="C#"
Inherits="System.Web.Mvc.ViewPage<Models.PropertyDetailsModel>"
MasterPageFile="../MvcMasterPage.Master" %>
Models.PropertyDetailsModel is highlighed in red and when I hover over it, it says that it can't resolve the symbol PropertyDetailsModel.
Ideas?

In your ~/web.config make sure that in the <assemblies> section you have the assembly containing this model:
<assemblies>
...
<add assembly="TheAssemblyContainingYourModel" />
</assemblies>
Then run the project to see if it works. Don't pay attention to the red squiggles. Visual Studio Intellisense in views is not to be trusted.
P.S: in ~/web.config there's also a <namespaces> section where you could put the namespace that contains your model which will DRY your views:
<pages>
<namespaces>
<add namespace="TheNamespaceContainingYourModel" />
</namespaces>
</pages>
and then in all your views in the entire application you can directly reference the models without the need to fully qualify them:
<%# Page Title="Title"
Language="C#"
Inherits="ViewPage<PropertyDetailsModel>"
MasterPageFile="../MvcMasterPage.Master" %>

Actually, all I had to do was change this:
Inherits="System.Web.Mvc.ViewPage<Models.PropertyDetailsModel>"
to this:
Inherits="System.Web.Mvc.ViewPage`1[Models.PropertyDetailsModel]"
Wacky!

Related

'The ADO.NET provider with invariant name 'Devart.Data.Oracle' is either not registered in the machine or application config file

Ok, there already is a similar issue. It is, however not exactly the same and the solution I got was not derived from the other issue's solution.
Here's my Web.Config setup:
<connectionStrings>
<add name="ADOEntities" connectionString="metadata=res://*/ADOModel.csdl|res://*/ADOModel.ssdl|res://*/ADOModel.msl;provider=Devart.Data.Oracle;provider connection string="User Id=dbUser;Password=*****;Server=oracleserver;Persist Security Info=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
Everything runs fine on my machine (sic), but when I tried to set the ws up on the quality server, I got the error on the title.
I got it working by following the steps in this ADO.NET link. Particularly,
You need to remove the defaultConnectionFactory registration and to add the Entity Framework provider registration by registering it in the entityFramework section
So the line defaultConnectionFactory must go
<entityFramework>
<providers>
<provider invariantName="Devart.Data.Oracle" type="Devart.Data.Oracle.Entity.OracleEntityProviderServices, Devart.Data.Oracle.Entity.EF6, Version=9.6.696.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
</providers>
</entityFramework>
Then, add the System.Data section. In my case it looks like this:
<system.data>
<DbProviderFactories>
<remove invariant="Devart.Data.Oracle" />
<add name="dotConnect for Oracle" invariant="Devart.Data.Oracle" description="Devart dotConnect for Oracle" type="Devart.Data.Oracle.OracleProviderFactory, Devart.Data.Oracle, Version=9.6.696.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
</DbProviderFactories>
</system.data>
If that still doesn't do the trick for you -- And it didn't for me -- try adding the following line to your context class:
[DbConfigurationType(typeof(Devart.Data.Oracle.Entity.OracleEntityProviderServicesConfiguration))] //Add this line
public partial class ADOEntities : DbContext
You might want to create a partial class, incase you're using ADO, Devart, or other auto-generated entity model, to avoid having this piece of code in an auto-generated class

Error with Strongly Typed Entity Framework Object in Razor View

Creating the razor view with strongly typed of Entity Framework generated POCO gives the below error.
The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
However i added System.Data, System.Data.Entity dlls into my MVC 3 project.
In case if i use ViewModel object instead of EF Created POCO it is not showing error.
What is the problem of binding Entity Framework object in Razor View?
Edit: Reading Using Entity Model Object in ASP.Net MVC View Page is not helping me. What could i do here, Do i need to create another ViewModel for each POCO?
Try adding
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
To the <system.web> section of your web.config

Is it possible to get standard ASP.NET MVC Unobtrusive Validation to work in Orchard CMS?

I'm trying to build a custom module to integrate with Orchard CMS to implement a business application. While Orchard CMS is an MVC application, it doesn't seem possible (or, at least easy) to do all the things that can be done "out of the box" with MVC.
I'm trying to get unobtrusive validation to work on my view but can't seem to get this to work.
Update: As per Rohan West's advice below, I've now got the scripts included in the page using the ResourceManifest class and the Script.Require calls.
However, the validation attributes on the actual HTML elements are not being generated despite having the .NET attributes on my properties for which I'm using #Html.EditorFor on.
I have set the appSettings in the web.config file as follows:
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
<add key="webpages:Enabled" value="false" />
<add key="log4net.Config" value="Config\log4net.config" />
</appSettings>
Still no joy!
Update 2: As per Rohan West's suggestion, modifying the OrchardStarter class to comment out the following lines "solves" the problem:
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new LocalizedModelValidatorProvider());
There should be a better way of handling this though.
You need to define the script in the resource manifest for your module.
public class ResourceManifest : IResourceManifestProvider
{
public void BuildManifests(ResourceManifestBuilder builder)
{
var manifest = builder.Add();
manifest.DefineScript("jQueryValidation").SetUrl("jquery.validate.js", "jquery.validate.min.js").SetVersion("1.7").SetDependencies("jQuery");
manifest.DefineScript("jQueryValidation_Unobtrusive").SetUrl("jquery.validate.unobtrusive.js", "jquery.validate.unobtrusive.min.js").SetDependencies("jQuery", "jQueryValidation");
}
}
and then in your page
#{
this.Script.Require("jQueryValidation_Unobtrusive").AtHead();
}
Have a look at the following class
Orchard.Environment.OrchardStarter
In Orchard 1.4.2 there is a line which removes all ModelValidatorProviders
ModelValidatorProviders.Providers.Clear();
This is removing the default DataAnnotationsModelValidatorProvider from the collection.
You could try adding it to the collection,

How to merge the applicationSettings of a class library into the host executable's config file

I am reading articles, forum posts about applicationSettings for almost a week now.
In almost every thread there was someone that appears to have correctly pointed out that the class libraries cannot have config files when deployed and their applicationSettings configured at design must be specified/merged in the executable.exe.config configuration file of the application that host/consumes the dll.
Not necessarily true.
You can but you don't need to merge them class library settings unless you want to provide the user with a way to "overwrite" the default values - the ones that are specified using the DefaultValueAttribute hard coded in the assembly.
So, for a very simple, practical example. Let's use VB.NET
1. I created a Class Library project called ClassLibrary.
2. Showing all files, expand MyProject, double click Settings.settings.
3. Adding a setting called Message, application scoped whose value is "Hello!".
4. Create a property in Class1.vb (the automatically added class)
Public Class Class1
Public Shared ReadOnly Property Message As String
Get
Return My.Settings.Message
End Get
End Property
End Class
Create a VB WinForms project and call it WinForm.
Add a reference to the ClassLibrary project.
Add a button to the already created Form1 and double click on it.
8 Add the some code to the the Button1_Click handler. Should look like this.
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MessageBox.Show(ClassLibrary.Class1.Message)
End Sub
End Class
Have WinForm "Set as Startup project"
Now, while in the IDE everything works beautifully. Run the solution and you'll get the expected Hello! when you press the button. If you go and change the setting in the app.config of the library to say "Good bye!" and you run the solution again you get a "Good bye!"
However, right click on the WinForm project and "Open in Explorer" and get to the Debug folder. There's no WinForm.exe.config file yet. Let's create one quickly.
Switch back to VS and while the WinForm project is selected click to Show All Files.
Expand MyProject, open Settings.settings, create a setting (doens't matter what) and save.
There we go, an App.config was created and if I build this solution, the Debug folder will contain a WinForm.exe.config.
Tell me how I merge my Message setting from the class library config
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ClassLibrary.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<ClassLibrary.My.MySettings>
<setting name="Message" serializeAs="String">
<value>Hello!</value>
</setting>
</ClassLibrary.My.MySettings>
</applicationSettings>
</configuration>
into the WinForm's config
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="WinForm.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
<applicationSettings>
<WinForm.My.MySettings>
<!--<setting name="A" serializeAs="String">
<value>A</value>
</setting>-->
</WinForm.My.MySettings>
</applicationSettings>
</configuration>
so I can change the Message setting's value in the WinForm.exe.config to something else and the application will display this new value overriding the DefaultValueAttribute.
I have done some research lately for this applicationSettings problem.
I found two relatively convenient ways of doing what I was asking for.
I put my thoughts about it together and wrote a blog entry here.
assemblies that your main assembly depend on can and will use settings you define in their individual projects but only during development just because they are build on the fly and contain whatever values you set last time. However when you deploy and think that you can place .config for each assembly they won't work if you expect to change a setting's value and have that reflected at runtime, that's because the last values are set as default values and are hardcoded in.
So you have to either move all your settings to the appSettings in the .config file of the main assembly and inject them at runtime as needed to the assemblies needing them (passing them as arguments).
Otherwise let's say these are your config files while in development:
2
3
For deployment you will have to create a configuration section in the main config file and point to
your modified "ClassLibrary.dll.config":
You don't have to point to external files though, the config sections declarations can point to their definitions further down in the main config file like this.

How to create the MVC equivalent of WIF PassiveSignInStatus Control?

I have included the below in my MVC View page
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Base.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%# Register TagPrefix = "idfx" Assembly = "Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace = "Microsoft.IdentityModel.Web.Controls" %>
<asp:Content ID="Content2" ContentPlaceHolderID="Content" runat="server">
<idfx:FederatedPassiveSignInStatus
ID="FederatedPassiveSignInStatus1"
runat="server"
OnSignedOut="FederatedPassiveSignInStatus1SignedOut"
SignOutText="Logout"
FederatedPassiveSignOut="true"
SignOutAction="FederatedPassiveSignOut" />
</asp:Content>
Since this is MVC, I cannot have runat=”server” attribute. When I remove the runat attribute, the page loads ok, but the control is not get displayed. How can I achieve this in MVC?
The Identity Training Kit or the samples in http://claimsid.codeplex.com/releases/view/62929 should show you ways of hooking WIF with MVC.
The problem is you cannot use event driven server-side controls in ASP.NET MVC like you can in ASP.NET webforms. If you are using webforms, than using the FederatedPassiveSignInStatus control to do sign-out is absolutely the easiest method.
In MVC we are going to have to issue the wa=wsignout1.0 command to the STS provider manually. If you look at the
Take a look at the 'FederationWithMultiplePartnersAndAcs' project in the Microsoft patterns & practices Cliams Identity Guide projects located on CodePlex here. If you look at the Logout() method on the HomeController, you will see the following code which does what we need:
public ActionResult Logout()
{
if (this.User.Identity.IsAuthenticated)
{
FederatedAuthentication.WSFederationAuthenticationModule.SignOut(false);
string issuer = FederatedAuthentication.WSFederationAuthenticationModule.Issuer;
var signOut = new SignOutRequestMessage(new Uri(issuer));
return this.Redirect(signOut.WriteQueryString());
}
return this.RedirectToAction("Index");
}
This is how you would need to implement the sign-out logic in MVC.

Resources