0

I am currently working on a 4 tier application, Core, Data, Shared, UI currently how it stands all our models are located inside the Shared project which is a class Library in one of the models we have the following implementation:

[Remote("UsernameUnique", "Register")]
[Required]
public string Username { get; set; }

This currently points to a Json method which is located inside the Register controller which is a part of the UI project as shown here,

public JsonResult UsernameUnique(UserRegistrationPartOne model)
{
    var t = model;
    return Json(false, JsonRequestBehavior.AllowGet);
}

But when I run this project and navigate to the sign up part it throws the error No url for remote validation could be found. I have been looking at the following just confirm I hadn't missed anything:

Remote Validaion inside MVC

According to what they do and what I've done is pretty much Identical, the only thing that I could think off which would be causing this problem is the fact the model is located in the Shared Project whilst its required on a view which is located in the UI Project? or maybe it could be something else?

Code Ratchet
  • 5,758
  • 18
  • 77
  • 141

2 Answers2

0

Your method needs to be

public JsonResult UsernameUnique(string Username)

A RemoteAttribute sends back the value of the property (not a model)

Side note: If you need to send back other properties of your model in order to do the validation, then you can use the AdditionalFields property of RemoteAttribute

  • I've done what has been suggested by both answers yet I still get the same error. – Code Ratchet Mar 27 '15 at 06:28
  • 1
    Do you have an areas? If so you need to specify the area with the 3rd parameter - `[Remote("UsernameUnique", "Register", "yourAreaName")]` - refer [documentation](https://msdn.microsoft.com/en-us/library/gg453821(v=vs.118).aspx) –  Mar 27 '15 at 06:33
  • that seemed to of done the trick thanks, when I run it and enter a username and navigate off the box i.e using tab it doesn't fire? by HTML looks like this – Code Ratchet Mar 27 '15 at 06:42
  • Is there any reason you have changed the `id` attribute from the default (which would be `Username`)? - always best to not change the id (but not sure it that might potentially cause a problem) –  Mar 27 '15 at 06:48
0
public JsonResult UsernameUnique(string userName)
{

    /// Checking your validation
    return Json(false, JsonRequestBehavior.AllowGet);
}

if you want add additional field you can add to Action like

public JsonResult UsernameUnique(string userName, FieldType additionalValue)

And Update the remote attribute like:

[Remote("UsernameUnique", "Register", AdditionalFields = "YourPropName"))]
Peyman
  • 3,068
  • 1
  • 18
  • 32
  • I have changed my remote attribute to look like yours, only difference YourPropName i've changed to Username, also updated my JsonResult to look like your's yet I still get the same error, do I need to specify any routes in the areaRegistration where this is being used? – Code Ratchet Mar 27 '15 at 06:27
  • You dont need to add Username as additional field. Username it pass by default as a first parameter. If you need send any more data, then should use AdditionalFields. If you want to send only username, dont change Attribute and only change the action like the first line – Peyman Mar 27 '15 at 06:28
  • If still have problem try to call Validation method manually, and see whats happen: /register/UsernameUnique/?userName=something – Peyman Mar 27 '15 at 06:30
  • I've just put that in the URL http://localhost:37495/register/UsernameUnique/?userName=something and it hits successfully, it complains when it gets the View is hits the username declaration and throws that error message – Code Ratchet Mar 27 '15 at 06:35