Mvc 远程属性正在查询字符串中发送“未定义”
本文关键字:未定义 字符串 查询 程属性 属性 Mvc | 更新日期: 2023-09-27 17:56:25
我正在尝试远程验证一些代码,对于参数,它作为参数传递undefined
。这是我的验证码:
[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);
}
}
这是我的模型:
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
}
看看Fiddler中的请求,这是我看到的:
GET /Validation/IsUserNameAvailable?UserName=sara&UserId=undefined
为什么 MVC 将字符串undefined
注入到请求中,而不是实际的 UserId?
你需要添加
@Html.HiddenFor(m=>m.UserId)
在视图中,以便绑定程序将其绑定到远程验证控制器,否则没有要绑定的值