I have created a project using ASP.Net MVC 5, EF 6 and .Net 4.5.1
At some point I needed to change the namespace that the project is in, from "MyTestProject" to "MyRealProject".
After making those changes throughout the web site I now get several errors in a couple of my views.
_ChangePasswordPartial.cshtml can't find "#model Microsoft.AspNet.Identity.ManageUserViewModel" any longer and _SetPasswordPartial.cshtml can't find "MyRealProject.ManageUserViewModel"
No where in the project can I find a file that contains the class ManageUserViewModel. Before I changed the namespace it was found but now it isn't. Why? Where did it go and how do I fix it?
Found out it's a known problem:
http://blogs.msdn.com/b/webdev/archive/2014/08/04/announcing-new-web-features-in-visual-studio-2013-update-3-rtm.aspx
When creating a default C# ASP.NET Web Application from MVC, WebAPI or SPA template with individual authentication, generated Views\Account\ _SetPasswordPartial.cshtml and _ChangePasswordPartial.cshtml files contain invalid model.
In file _SetPasswordPartial.cshtml,
#model .Models.ManageUserViewModel
Should be changed to:
#model .Models.SetPasswordViewModel
In file _ChangePasswordPartial.cshtml,
#model Microsoft.AspNet.Identity.ManageUserViewModel
Should be changed to:
#model .Models.ChangePasswordViewModel
Similar problems exist for generated VB projects as well.
In file _SetPasswordPartial.vbhtml,
#ModelType ManageUserViewModel
Should be changed to:
#ModelType SetPasswordViewModel
In file _ChangePasswordPartial.vbhtml,
#ModelType ManageUserViewModel
Should be changed to:
#ModelType ChangePasswordViewModel
Also posted it here:https://stackoverflow.com/a/27687882/1071698. I don't know what the rules with duplicate questions and answers, please edit as necessary.
_ChangePasswordPartial.cshtml should use something like
#model MyRealProject.Models.SetPasswordViewModel
Then make sure in the models folder AccountViewModels.cs
The namespace is updated to MyRealProject.
Hope that helps.
CRTL + SHIFT + F , then type in "class ManageUserViewModel" , just that simple
make sure it is set to search entire solution , and it will find the class you are looking for
Related
I created a new project in C# using dotnet new mvc. I noticed under my models folder a file called ErrorViewModel.cs. I was wondering why its called that as it looks like its for something specific like errors or some other such thing.
Would it be safe to change the file name to something else?
This link will answer my own question.
Its basically there to handle errors such as 404 and other such things.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-5.0
I am using ASP.NET MVC5 and I would like to render a view from a folder that is outside the application folder. I tried registering my own custom the VirtualPathProvider and I even created my own VirtualPathProviderViewEngine to support rendering pure html pages. I have the latter working but cannot get the former to work. When I navigate to the route in question, I want MVC to check the internal Views folder for the View and then if it is not found I want it to look in the external folder.
When I step through the code, FileExists gets called for files that are in the Views folder and then the ViewEngine code runs but for a View that lives externally, the FileExists check runs and then I get a 404 on the screen. It does not ever get into the ViewEngine code. I know I am missing something simple here.
I am attaching a screenshot of what the sample folder structure would be. Any help would be greatly appreciated.
You can override the VirtualPathProvider and the VirtualFile
Check this link for an Example
I saw some Tutorials on ASP.Net MVC HtmlHelpers and they always included the HTML directly into the SourceCode.
I want to create reusable Controls so that I don't have to write a Login view and the parts of it over and over again over the next projects.
The best thing would be if I could write a DLL and place all my created user controls therein
Some time ago I wrote an application with AngularJS and there were directives
and in them was a templateUrl. Is there something similar in Asp.Net MVC ?
I am using the Razor View Engine and the .Net Framework 4.0.
I know I could use partial views but partial views seem to not work in dlls
"The best thing would be if I could write a DLL and place all my created user controls therein" - You can. There is one little cheat which makes it all work really easily.
When you are writing your html helpers, make sure that you change the namespace to System.Web.Mvc.Html.
If you use the TagBuilder class then you shouldn't be using too much html in your C# code.
Then if you reference your dll in the project, you should be able to access the html helper from your razor view
You can use other namespaces, but you have to have to edit the web.config file inside the Views folder and add a reference to the namespace in the <system.web.webPages.razor> section. By re-using the already referenced namespace, you can save yourself some configuration hassles.
Depending on how many projects and how many developers you want to share the code between, you could also consider a build server product (My team used TeamCity for about 2 years before we needed to pay for a licence). You can then produce your own custom NuGet packages, which lets you share (and manage updates) for partial views, editor templates, html helpers and much more.
Wondering if anyone else has experienced this and what their solution was if so. In Visual Studio 2013 I create a new ASP.NET Web Application, leaving all the defaults as they are
In the next screen I pick MVC, adding folders and core references for MVC but not the other two options. Authentication is left at Individual User Accounts and I've unchecked the Host in the cloud option, as shown below.
The project wizard completes and I can see that there are 26 errors in it before I do anything else.
The first fix that removes a bunch of these errors is that the Views\Account\ _SetPasswordPartial.cshtml and _ChangePasswordPartial.cshtml files contain invalid models so I change those as follows:
[My project name here is WebApplication1, substitute your own value]
In _SetPasswordPartial.cshtml:
From #model WebApplication1.Models.ManageUserViewModel to #model WebApplication1.Models.SetPasswordViewModel
In file _ChangePasswordPartial.cshtml:
From #model Microsoft.AspNet.Identity.ManageUserViewModel to #model WebApplication1.Models.ChangePasswordViewModel
That drops me down to 4 errors, spread across 4 files
1.
Line 68, ManageController.cs
return View(linkedAccounts);
The view RemoveLogin doesn't exist
2,3.
There are two errors _SetPasswordPartial.cshtml and _ChangePasswordPartial.cshtml complaining about not being able to resolve the Manage action but when I debug and visit those URLs in the browser they work fine so I suspect they're in a route table somewhere. I have R# installed so sometimes that can be wrong if that's the case.
4.
The last one is that the _RemoveAccountPartial.cshtml has an error on line 15 where it complains about not having a Disassociate action in the Account controller, as far as I can ascertain this is to do with removing other authentication providers to the application.
Now I can fix all these by adding the required code but it just doesn't sit well with me that the templates don't work well out of the box. Are there fresh templates available or has anyone done the canonical write-up on how to get your template humming before you commence work properly on it?
EDIT 2014-11-13
I've just applied VS2013.4 and these issues appear to be fixed as part of it. If this is an issue for people then I suggest applying that update.
I do not know what is the root cause of these problems but after playing with the project the following seems to bring it to a consistent state:
Remove Views\Account\_SetPasswordPartial.cshtml
Remove Views\Account\_ChangePasswordPartial.cshtml
Remove Views\Account\_RemoveAccountPartial.cshtml
All these have counterparts under the Manage controller. And finally:
Remove the RemoveLogin() action method from ManageController. Make sure you only remove the GET method (lines 64 - 69), as the POST one is actually used. The list of logins is rendered by ManageLogins action.
Found out it's a known problem:
http://blogs.msdn.com/b/webdev/archive/2014/08/04/announcing-new-web-features-in-visual-studio-2013-update-3-rtm.aspx
When creating a default C# ASP.NET Web Application from MVC, WebAPI or SPA template with individual authentication, generated Views\Account\ _SetPasswordPartial.cshtml and _ChangePasswordPartial.cshtml files contain invalid model.
In file _SetPasswordPartial.cshtml,
#model .Models.ManageUserViewModel
Should be changed to:
#model .Models.SetPasswordViewModel
In file _ChangePasswordPartial.cshtml,
#model Microsoft.AspNet.Identity.ManageUserViewModel
Should be changed to:
#model .Models.ChangePasswordViewModel
Similar problems exist for generated VB projects as well.
In file _SetPasswordPartial.vbhtml,
#ModelType ManageUserViewModel
Should be changed to:
#ModelType SetPasswordViewModel
In file _ChangePasswordPartial.vbhtml,
#ModelType ManageUserViewModel
Should be changed to:
#ModelType ChangePasswordViewModel
I confirmed issue also in VS2013 Ultimate Update#3. Submitted bug report to Microsoft Connect under Visual Studio and .NET Framework section.
I've been learning MVC3 a bit for a while and although I understand most of the basics, the routing is something I just can't seem to get a hang off. I'm not sure whether I'm doing/thinking it in a correct way and the other answers I've read on stackoverflow/google only seem to confuse me more.
Anyway here's the situation. I've got a solution (cleaned it up for this question), where I'd like to create a structure in my views folder to organize everything in a structure I've been using for my asp.net webforms projects.
Anyway, I was wondering whether it would be possible to get this kind of structure, where I could organize all my partial views into a specific subfolder called UserControls in which there are more subfolders where I would group every partial view I'd need for a specific page.
Ideally, my Views folder would contain 2 folders: Pages and UserControls and everything I'd need would go into a subfolder, or a sub sub folder.
Okay so if this is possible, how would I start routing this? I've been trying multiple ways of getting any result but they all end up in a 404 errors.
If this is a wrong approach of me, what would be a better alternative?
Thanks in advance!
Logically, this is better served by grouping your partials in with the actual views like so:
/Other
/Partials
OtherPartial1.cshtml
OtherPartial2.cshtml
Index.cshtml
/Some
/Partials
SomePartial.cshtml
Index.cshtml
SomeOtherView.cshtml
Now, you can certainly do as you suggest, but it simply means that your helpers in other views have more text to write to get where they want to go:
IE.
#RenderPartial("Partials/OtherPartial.cshtml")
VS
#RenderPartial("~/Views/PartialControls/Other/OtherPartial.cshtml")
You could manually return the view file for each actionresult or partialactionresult.
I'd also recommend changing "UserControls (PartialViews)" folder to just PartialViews.
e.g.
public PartialViewResult OtherPartialView1()
{
return PartialView("../PartialViews/OtherViews/OtherPartialView.cshtml");
}