2

I have a Register and login page both using unobtrusive ajax.
The Username property in my model is decorated with Remote("ActionName","ControllerName")
Its working fine in the Registration Page but the problem is the validation is also working in the Login Page. So how can i disable the Remote validation attribute on the Login Page but i do want the ajax functionality of signIn in the Login Page so i cant remove the unobtrusive javascript file

Vivek
  • 661
  • 1
  • 12
  • 23

1 Answers1

1

You cannot turn off Remote validators dynamicly.

The solution is to not use the same model for the two view.

Instead of create two viewmodels one for login and on for the register view and annotate them differently:

public class RegisterUserViewModel
{
    [Remote("ActionName","ControllerName")]
    public string Username { get; set; }

    //...
}

public class LoginUserViewModel
{
    public string Username { get; set; }

    //...
}

For mapping properties from your viewmodel to your models in the controller you can use some object-object mapper like AutoMapper

nemesv
  • 138,284
  • 16
  • 416
  • 359
  • Thanks for your quick response. I have guessed the same but its this way it keeps on increasing the number of ViewModel. Is it the best practice ??? by the way i have accepted as an answer.however it wud be nice to dynamically turn off `Remote` validator – Vivek May 05 '12 at 08:38
  • Don't worry about creating lots of ViewModels, it's a best practice to use them. It helps the separation of concerns that each view has it's own tailored ViewModel for its specific needs. Like in your example on one page you need validation on another page you don't. You can start read about viewmodels e.g [here](http://stackoverflow.com/questions/6157348/when-do-i-use-view-models-partials-templates-and-handle-child-bindings-with-mv) and [here](http://www.rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications). – nemesv May 05 '12 at 08:45
  • About turn off `Remote` : maybe (I haven't tried) with JavaScript you can dynamically remove the MVC generated `data-` attributes from your `input` elements (which are used to controlling the validators), but I really don't suggest to take that approach. – nemesv May 05 '12 at 08:49