what is namespace for Compare attribute MVC 4? - asp.net-mvc

i need to write a registration form and need to [Compare] attribute for my Confirm Password field.
i'm using MVC 4 .
Is there this attribute in mvc 4?
if answer is yes what namespace need to use?

As of MVC4, the [Compare] attribute is potentially in two different namespaces:
System.Web.Mvc
System.ComponentModel.DataAnnotations (with Framework 4.5).
The Mvc version is IClientValidatable and all the benefits that brings - front end validation etc..
With Asp.NET Mvc 5 the System.Web.Mvc one is marked as obsolete, which may be something to bear in mind if you are going to be migrating upwards soon.
If your model mixes the two namespaces above, you can expicitly choose which one to use my using the full namespace - e.g. [System.Web.Mvc.Compare( .. )]

Related

SelectList 4 signatures vs 9, oh dear

Ref:
http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist(v=vs.118).aspx
I have a VS Solution that contains the following ASP.NET MVC projects:
Company.Domain
Company.WebUI
Company.UnitTests
Under Company.Domain I put my models, modules and abstract classes. I have one module where I generate and return a SelectList object. This SelectList object takes several parameters of which one is GROUP. If I move this class to my Company.WebUI project (which is a ASP.NET MVC 5 web app) and I hover my mouse over the SelectList keyword I see it accepts 9 signatures. However, if I hover my mouse over the same keyword in my Company.Domain (a class based project) I see only 4 possible signatures.
SelectList requires System.Web.MVC which is imported into the class.
Weird problem.
How can I fix this under Company.Domain?
Edit:
This is the line it's failing on, it's complaining "Overload resolution failed because no accessible NEW accepts this number of arguments."
AddToCache(aKey, New SelectList(GetListFromDB(SQL), "Value", "Text", "Group", Nothing, Nothing))
ANSWERED:
The question was answered by #BilalFazlani. Domain was running System.Web.Mvc version 4.0 and WebUI running 5.2.2. Updated Domain and now all works.
Check the version of MVC you have referenced in Domain project and compare it with MVC version of WebUI.
The newer version of MVC has more overloads of SelectList compared to the older version

Can you split an MVC Area across two projects?

I have an Asp.Net MVC 3 website written in VB.Net. We want to migrate to C#, the plan being to write all new code in C#, and rewriting old code as and when it's amended.
I created a new C# MVC 3 project in the solution. Within this project I am using Razor Generator to help compile the views into the resulting assembly. I reference the C# project from the VB one. The VB project runs as the main site.
This set up works 90% beautifully. However, if an Area already exists in the VB project, I can't seem to extend it in the C# project. It appears that the whole Area has to exist in either one project or the other.
Does anyone know if it is possible to serve 1 area from 2 projects?
I had to work around this by creating a route in the area registration of the VB.Net project.
The Area Registration file in the C# project needed to be removed, and the route in the VB.Net project uses a slightly different URL pattern. When creating the new route the Namespace that the C# controllers reside in need to be set. The new route also needs to be declared before the default route for the area.
It is entirely possible, and what you are doing is correct. I would surmise that your routes are not set up correctly. You will have to specify the namespaces in the routing in your VB project.
In your AreaRegistration code:
Public Overrides Sub RegisterArea(ByVal context As AreaRegistrationContext)
context.MapRoute(
"Users_default",
"Users/{controller}/{action}/{id}",
New With {.action = "Index", .id = UrlParameter.Optional},
New String() {"MyCompany.MyAmazingCSProject.Areas.Users.Controllers",
Me.GetType().Namespace}
)
End Sub
Remember that VB namespaces have the added complication of root namespaces, which work differently to the Default namespace of C# projects, so if you're working with both, you need to be consistent.
i.e.
A VB project with a root namespace of MyCompany.MyAmazingVBProject has this namespace in code:
Namespace Areas.Users.Controllers
which maps to MyCompany.MyAmazingVBProject.Areas.Users.Controllers, whereas the equivalent namespace in C# would have this code:
namespace MyCompany.MyAmazingCSProject.Areas.Users.Controllers { ... }

Data Annotations and MVC 4

I did MVC 2 two years ago. I am now using MVC 4 and would like to confirm a few things.
1) In MVC 4, we do not need to create a ModelMetadata class to annotate with data annotations for data validation. We may simply annotate the model classes themselves.
In other words, if we are using the EDM generator to generate a model and corresponding classes for us, then we may create a new set of partial classes with the same names and use data annotation attributes on those partial classes themselves.
We do not need to create a new type of model metadata class and decorate that class with data annotation/attributes, like here: http://www.asp.net/mvc/tutorials/older-versions/models-%28data%29/validation-with-the-data-annotation-validators-cs
2) For data annotations to work, we need a reference only to System.ComponentModel.DataAnnotations and not to Microsoft.Web.Mvc.DataAnnotations.
3) We do not need to instantiate the default model binder in the Application_Start event in the Global.asax file.
Could you please confirm if my understanding is correct?
As I remember, there's no difference between MVC 2 and MVC 4 when it comes to this. You didn't have to use MetadataType in MVC 2, you could have used partial classes for generated models.
I'm not able to find any MSDN doc on Microsoft.Web.Mvc.DataAnnotations, but for data annotations to work with MVC you'd need System.ComponentModel.DataAnnotations and System.Web.Mvc (it contains some additional attributes like HiddenInputAttribute).
No, you don't, it's instantiated by framework.
Actually, for question 1, you need to use Metadata classes to annotate the properties of of model-first EF classes in the same way as previously with MVC2. When you use code-first EF classes, you can annotate them directly - the annotations will be used for generating the database as well as for scaffolding the Views.

Is there a way to rename the RequestVerificationToken cookie name?

Using ASP.net MVC v2.0, Any way to change the name of the __RequestVerificationToken cookie? In an effort to conceal our underlying technology stack, I’d like to rename the cookie to something that can’t be traced back to ASP.Net MVC.
More info on this at Steve Sanderson's blog.
ASP.NET MVC 3 and 4 let you change the cookie name by setting the static AntiForgeryConfig.CookieName property.
(Msdn reference here)
I know that the question asks specifically about ASP.NET MVC 2, but this question still returns high up the search engine rankings for appropriate queries such as "ASP.NET MVC AntiForgeryToken cookie name". I thought I'd add the information here to save others from decompiling the ASP.NET MVC 3+ source code like I did.
Looking at the MVC 2 source code I dont think it's possible to change the cookie name. The AntiForgeryData class starts:
private const string AntiForgeryTokenFieldName = "__RequestVerificationToken";
and to get the cookie name it just calls:
string cookieName = AntiForgeryData.GetAntiForgeryTokenName(ViewContext.HttpContext.Request.ApplicationPath);
in the HtmlHelper class. It takes the application path and converts it to base 64 and appends it onto the end of __RequestVerificationToken which is what you see when you view the source.
If you really need to change the name I'd recommend downloading the MVC 2 source code from codeplex and look at creating your own html helper and anti forgery token using the source code as a reference. But in doing this you could always introduce your own bugs...

Choose the best Client/Server Validation Framework for MVC

Im trying to choose the best server/client validation framework to use with my MVC application. I would like to know what is the best validation framework to use with MVC to do server/client validation.
My options are:
Castle validator
Microsoft Data Annotations
nHibernate Validator
With the upcoming MVC 2, it looks like MS is leaning towards the System.ComponentModel.DataAnnotations library. It's pretty nice - does a lot of code generation for you.
I've been using xVal with great success.
The best thing is you can fully automate validation:
put DataAnnotations (or special rules) to your business layer POCO classes (if you have them) or entity classes' metadata buddy classes if you use entities up to controller layer
write an ActionFilter that will automatically validate parameters to your controller actions - it's best if all your POCOs (or entities) implement a certain interface that defines Validate() method, filter calls this method and fills ModelState.Errors when validation fails.
Add if (ModelState.IsValid) { ... } in your controller action that needs to work differently when model isn't valid
Put <%= Html.ValidationMessage(...) %> in your views that will display validation errors
Add xVal's client validation to your views if desired
This way you've set up automatic object validation. All your controller actions will do is check Model validity.
Asp.net MVC 2 will have something very similar to xVal already built in the framework so it's up to you which version are you using.

Resources