15

I have a validation controller within the route of my project that I'm trying to use from within an area using the following attribute on the model's property...

    [Remote("IsValidUserName", "Validation", "", ErrorMessage = "Invalid username")]

But when this is rendered, the validation is against the "IsValidUserName" action of controller "Validation" within the same area as the page, and not within the root area...

data-val-remote-url="/Members/Validation/IsValidUserName"

Any help would be appreciated.

Thanks.

user644344
  • 885
  • 1
  • 7
  • 7

2 Answers2

31

Unfortunately that's how this attribute is implemented. Here's an excerpt from the constructor of this attribute:

public RemoteAttribute(string action, string controller, string areaName) : this()
{
    if (string.IsNullOrWhiteSpace(action))
    {
        throw new ArgumentException(MvcResources.Common_NullOrEmpty, "action");
    }
    if (string.IsNullOrWhiteSpace(controller))
    {
        throw new ArgumentException(MvcResources.Common_NullOrEmpty, "controller");
    }
    this.RouteData["controller"] = controller;
    this.RouteData["action"] = action;
    if (!string.IsNullOrWhiteSpace(areaName))
    {
        this.RouteData["area"] = areaName;
    }
}

Notice the IsNullOrWhiteSpace test against the areaName at the end that's killing everything?

You could fix it by writing a custom remote attribute:

public class MyRemoteAttribute : RemoteAttribute
{
    public MyRemoteAttribute(string action, string controller, string area)
        : base(action, controller, area)
    {
        this.RouteData["area"] = area;
    }
}

and then:

[MyRemote("IsValidUserName", "Validation", "", ErrorMessage = "Invalid username")]
public string Username { get; set; }

Now the proper data-val-remote-url="/Validation/IsValidUserName" will be generated.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • @user644344, it is available here: http://aspnet.codeplex.com/releases/view/58781. But you can also use Reflector to quickly browse through assemblies. – Darin Dimitrov Mar 13 '11 at 19:42
  • 2
    Or as a free alternative to Reflector I'd recommend [ILSpy](http://wiki.sharpdevelop.net/ILSpy.ashx). – MHollis Jun 27 '11 at 16:01
  • I got this to generate correct data-val-remote-url. however when I debug it still does not fire the correct action in the root controller. It does works with in the area when I use Remote Attr. but does not go to root level controller when I use MyRemote Attribute! Any help is much appriciated. thx – activebiz Aug 24 '11 at 08:09
  • Just to let you know I found the answer myself. The problem was there were two controllers with the same name in the Application. One in the Area and another one in the root. Does it mean we can not have two controllers with the same name at application level? – activebiz Aug 24 '11 at 11:27
  • 1
    Dont worry about this either I found answer myself and here it is http://stackoverflow.com/questions/5065422/is-it-possible-in-mvc3-to-have-the-same-controller-name-in-different-areas – activebiz Aug 24 '11 at 11:32
  • This has been fixed in ASP.NET Core, where RemoteAttribute uses a similar constructor as shown in this fix. – Tom Lint Jun 15 '16 at 09:35
9

I came upon this same issue and found a solution that works for me. The remote attribute takes an AreaReference Enum.

System.Web.Mvc.AreaReference is an ENUM which has two values UseRoot & UseCurrent more details found here

Example useage that works for me:

[Remote("IsValidUserName", "Validation", System.Web.Mvc.AreaReference.UseRoot, ErrorMessage = "Invalid username")]
0m3r
  • 12,286
  • 15
  • 35
  • 71
CodeMage
  • 156
  • 1
  • 1