Remote Validation Doesn’t Transfer Data to Action

I have a model:

public class MyModel ...fields [Remote(ActionName, ControllerName)] public string SomeNumber { get; set; } ..fields 

And do the action in ControllerName:

 public JsonResult ActionName(string someNumber) {...} 

But when the action is called, the someNumber parameter is always null. And when I try to debug it, I get

 GET /ControllerName/ActionName?MyModel.SomeNumber =34189736 

How can I make it work? (I cannot pass the entire MyModel model and cannot change the name of the MyModel.SomeNumber field in my view)

UPD Input in my opinion:

 <input data-val="true" data-val-remote-additionalfields="*.SomeNumber" data-val-remote-url="/ControllerName/ActionName" id="MyModel_SomeNumber" name="MyModel.SomeNumber" type="text" value="34189734" class="valid"> 

UPD solved! :) I am creating a new model with one SomeNumber field and a prefix of use:

 SomeNumber([Bind(Prefix = "MyModel")]MySingleFieldModel model) 
+4
source share
3 answers

If you use a nested ViewModels , you need to accept the parent ViewModel as an argument in your Validation action. For instance:

 public class ParentViewModel { public UserViewModel User {get; set; } //.... } public class UserViewModel { [Remote("UniqueUsername", "Validation")] public string Username { get; set; } //.... } 

In ValidationController:

 public class ValidationController : Controller { public JsonResult UniqueUsername(ParentViewModel Registration) { var Username = Registration.User.Username; //access the child view model property like so //Validate and return JsonResult } } 
+5
source

Try using the model as a parameter. So that he can attach a value to it.

 public JsonResult ActionName(MyModel model) { //... model.SomeNumber; //... return Json(validationResult, JsonRequestBehavior.AllowGet) } 
+2
source
 public JsonResult ActionName(string SomeNumber) {...} 

I think you might need to match case with your input parameter.

0
source

All Articles