Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Remote attribute method parameter always passing null

I have this AdvertiserNameAvailable method that is being used by Remote validation attribute. The problem is that the AdvertiserNameAvailable is being called without passing the input value to the method Name parameter. When I enter in debug into the method, 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;
      }
  }
like image 412
user1662812 Avatar asked Nov 11 '12 11:11

user1662812


1 Answers

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 do inspect element on the input that you are trying to validate. Look for the name attribute:

<input ... id="account_Name" name="account.Name" type="text" value="">
like image 143
user1662812 Avatar answered Sep 27 '22 20:09

user1662812