【发布时间】:2015-06-15 15:47:47
【问题描述】:
如果我通过RemoteAttribute 指定AdditionalFields 进行验证以处理可能的可选附加字段,如何配置操作?目前我尝试对可选字段的可能组合进行操作,但它不起作用,如下所示。
我有一个绑定到模型的表单,其中一个字段是用户名的远程验证字段。当我运行程序时,我收到一个错误。这是我的验证码:
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public class ValidationController : Controller
{
public JsonResult IsUserNameAvailable(string userName)
{
// compute condition, only "fail" shown.
return Json(string.Format("{0} is already taken.", userName),
JsonRequestBehavior.AllowGet);
}
public JsonResult IsUserNameAvailable(string userName, string UserId)
{
// compute condition using both name and ID, only "fail" shown.
return Json(string.Format("{0} is already taken.", userName),
JsonRequestBehavior.AllowGet);
}
}
这是我的模型:
public class EditUserAdministrationViewModel
{
public int UserId { get; set; }
[Required(ErrorMessage = "You must enter a first name.")]
[Display(Name = "First Name")]
public string FirstName { 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 fileds
}
确实我得到了运行时错误(因为Routing: The current request for action [...] is ambiguous between the following action methods 中涵盖了 2 个按名称匹配的操作):
当前对控制器类型“ValidationController”的操作“IsUserNameAvailable”的请求在以下操作方法之间不明确:
BdsManager.Controllers.ValidationController 类型上的 System.Web.Mvc.JsonResult IsUserNameAvailable(System.String)
BdsManager.Controllers.ValidationController 类型上的 System.Web.Mvc.JsonResult IsUserNameAvailable(System.String, System.String)
【问题讨论】:
-
发送的实际请求或查询字符串是什么样的?
-
你真的需要在帖子中展示SQL注入示例吗?我认为这与您的问题无关。
-
@AlexeiLevenkov - 我的代码不对 SQL 注入开放。
GetBySqlStatement参数化了一切。 -
这里链接的问题根本不是重复的。链接的问题与远程属性参数无关,都是关于路由的。
-
请确保编辑您的问题以澄清这一点 - 到目前为止,看起来像 MVC 路由匹配 2 个请求操作的基本问题。可能您可以减少样本以同时删除 SQL 内容。
标签: c# asp.net asp.net-mvc asp.net-mvc-4