[英]Mvc Remote Attribute is sending “undefined” in query string
I'm trying to remote validate some code and for the parameter, its passing undefined
in as a parameters. 我正在尝试远程验证一些代码,并对该参数进行传递,并将其
undefined
地传递为参数。 Here is my validation code: 这是我的验证代码:
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public class ValidationController : Controller
{
public JsonResult IsUserNameAvailable(string userName, int? UserId)
{
var users = new BusinessLayer.BdsAdmin.Users();
if (UserId == null || UserId == 0)
// Do something
else // Do something else
if (users.Count == 0)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
string msg = string.Format("{0} is already taken and is not available.", userName);
return Json(msg, JsonRequestBehavior.AllowGet);
}
}
Here is my model: 这是我的模型:
public class EditUserAdministrationViewModel
{
public int UserId { get; set; }
[Required(ErrorMessage = "You must enter a user name.")]
[Display(Name = "User Name")]
[Remote("IsUserNameAvailable", "Validation", AdditionalFields = "UserId")]
public string UserName { get; set; }
// More properties
}
Looking at the request in Fiddler, here is what I see: 在Fiddler中查看请求,这是我看到的内容:
GET /Validation/IsUserNameAvailable?UserName=sara&UserId=undefined
Why is MVC injecting the string undefined
into the request instead of the actual UserId? 为什么MVC将
undefined
的字符串而不是实际的UserId注入到请求中?
You need to add 您需要添加
@Html.HiddenFor(m=>m.UserId)
at the view so that the binder will bind it to the remote validation controller or otherwise there is no value to bind 在视图上,以便绑定器将其绑定到远程验证控制器,否则就没有绑定的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.