C# Mvc远程属性正在发送";“未定义”;在查询字符串中
我试图远程验证一些代码,对于参数,它作为参数传入C# Mvc远程属性正在发送";“未定义”;在查询字符串中,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我试图远程验证一些代码,对于参数,它作为参数传入undefined。这是我的验证代码: [OutputCache(Location = OutputCacheLocation.None, NoStore = true)] public class ValidationController : Controller { public JsonResult IsUserNameAvailable(string userName, int? UserId) { var
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将字符串未定义的注入到请求中,而不是实际的用户ID?您需要添加
@Html.HiddenFor(m=>m.UserId)
在视图中,以便活页夹将其绑定到远程验证控制器,否则没有要绑定的值您需要在view@DanHunex“那完全正确。非常感谢。给我一个答案,我就接受!