The parameter name in the validation action of the remote MVC3 model

I use the Remote validation attribute for the SSN property, on the browse page I use the general view, then the ssn field looks like this:

 @Html.EditorFor(model => model.MainModel.SSN) @Html.ValidationMessageFor(model => model.MainModel.SSN) 

and My Action:

 public JsonResult IsValidaSSN(string SSN) { //.... return Json(result, JsonRequestBehavior.AllowGet); } 

but always the SSN is null in action, I also try MainModelSSN , MainModel_SSN , but unchanged and always null, what is your suggestion? What is the correct name for MainModel.SSN in the action argument?

+4
source share
3 answers

You can try to specify a prefix:

 public Action IsValidaSSN([Bind(Prefix = "MainModel")] string SSN) { //.... return Json(result, JsonRequestBehavior.AllowGet); } 

MainModel is the prefix that is used to send data => MainModel.SSN .

+4
source

I solved this problem by simply using the first query string parameter:

 public JsonResult IsValidImageUrl(string value) { if (value == null && Request.QueryString.Count == 1) { value = Request.QueryString[0]; } //.... return Json(result, JsonRequestBehavior.AllowGet); } 
+3
source

Another, slightly more accurate answer to Rahul's question:

 public JsonResult IsValidImageUrl(string SSN) { if (string.IsNullOrEmpty(SSN)) { string parmname = Request.QueryString.AllKeys.FirstOrDefault(k => k.EndsWith(".SSN")); if (!string.IsNullOrEmpty(parmname)) { SSN = Request.QueryString[parmname]; } } //.... etc. 

It can fly with several parameters.

Note that you will probably want to reconsider the association of the GET JSON method with everything related to SSN.

+2
source

Source: https://habr.com/ru/post/1412124/


All Articles