ASP.NET MVC Remote attribute method parameter, always null

I have this AdvertiserNameAvailable method that is used by the Remote validation attribute. The problem is that the AdvertiserNameAvailable call is called without passing an input value to the Name method parameter. When I enter the method debugging, I see that the Name parameter is always null .

  public JsonResult AdvertiserNameAvailable(string Name) { return Json("Some custom error message", JsonRequestBehavior.AllowGet); } public class AdvertiserAccount { [Required] [Remote("AdvertiserNameAvailable", "Accounts")] public string Name { get; set; } } 
+4
source share
2 answers

I had to add [Bind(Prefix = "account.Name")]

 public ActionResult AdvertiserNameAvailable([Bind(Prefix = "account.Name")] String name) { if(name == "Q") { return Json(false, JsonRequestBehavior.AllowGet); } else { return Json(true, JsonRequestBehavior.AllowGet); } } 

To find out your prefix, right-click and check the item at the input you are trying to check. Find the name attribute:

 <input ... id="account_Name" name="account.Name" type="text" value=""> 
+12
source
 [HttpPost] [OutputCache(Location = OutputCacheLocation.None, NoStore = true)] public ActionResult AdvertiserNameAvailable(string Name) { bool isNameAvailable = CheckName(Name); //validate Name and return true of false return Json(isNameAvailable ); } public class AdvertiserAccount { [Required] [Remote("AdvertiserNameAvailable", "Accounts", HttpMethod="Post", ErrorMessage = "Some custom error message.")] public string Name { get; set; } } 

It should also be noted:

The OutputCacheAttribute attribute is required to prevent ASP.NET MVC from caching the results of validation methods.

So use [OutputCache(Location = OutputCacheLocation.None, NoStore = true)] in your controller action.

0
source

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


All Articles