【问题标题】:MVC Remote validation is adding model to urlMVC 远程验证正在将模型添加到 url
【发布时间】:2017-04-21 15:53:47
【问题描述】:

我对一个字段进行了远程验证,它会触发,但我收到一个 500 错误,说明控制器方法为 null 时参数为 null。实际发生的是模型名称被添加到 url 中,这使得它无法找到方法。

例子:

学生模型

    [Remote("CheckExistingStudent", "Student", AdditionalFields = "FirstName", ErrorMessage = "This student already exists!")]
    public string LastName { get; set; }
    public string FirstName { get; set; }

由于某种原因,验证触发时创建的 url 是:

http://localhost:4855/Student/CheckExistingStudent?Student.LastName=Waters&Student.FirstName=Nigel

注意学生。在网址中。

我尝试了几种不同的方法签名:

    public JsonResult CheckExistingResource(string FIrstName, string LastName)

    public JsonResult CheckExistingResource(StudentModel model)

第一个返回500错误(空参数)。第二个通过 Student 模型进入他的方法,但第一个和最后一个为空。

我确信这正在发生,因为我正在使用 Html.BeginCollectionItem 帮助器来构建动态列表,以便用户可以在同一页面上输入多个学生。 BCI 帮助程序的一部分是它使用唯一索引重命名每个字段。

所以我认为我不能阻止 url 具有模型。在里面。是否可以有模型。在方法参数名称中?我试过了,出错了。

【问题讨论】:

    标签: c# asp.net-mvc begincollectionitem


    【解决方案1】:

    也许模型绑定前缀是有序的:

    public JsonResult CheckExistingResource([Bind(Prefix="Student")]StudentModel model)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 2017-03-12
      • 1970-01-01
      相关资源
      最近更新 更多