ASP.NET MVC远程属性的方法参数总是传递null [英] ASP.NET MVC Remote attribute method parameter always passing null

查看:127
本文介绍了ASP.NET MVC远程属性的方法参数总是传递null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有正在使用的远程验证属性,这个 AdvertiserNameAvailable 方法。
问题是,在 AdvertiserNameAvailable 是被称为无输入值传递给方法名称参数。当我在调试进入的方法,我看到名称参数始终是

 公共JsonResult AdvertiserNameAvailable(字符串名称)
  {
      返回JSON(一些自定义错误消息,JsonRequestBehavior.AllowGet);
  }  公共类AdvertiserAccount
  {
      [需要]
      [远程(AdvertiserNameAvailable,账户)]
      公共字符串名称
      {
          得到;
          组;
      }
  }


解决方案

必须添加 [装订(preFIX =account.Name)]

 公众的ActionResult AdvertiserNameAvailable([装订(preFIX =account.Name)]字符串名称)
{
    如果(名称==Q)
    {
        返回JSON(假,JsonRequestBehavior.AllowGet);
    }
    其他
    {
        返回JSON(真,JsonRequestBehavior.AllowGet);
    }
}

要找出你的preFIX,点击右键,请在您要验证输入检查元素。查找名称属性:

 <输入... ID =帐户名NAME =account.Name类型=文本值=>

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;
      }
  }

解决方案

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="">

这篇关于ASP.NET MVC远程属性的方法参数总是传递null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆