简体   繁体   English

MVC远程验证正在向网址添加模型

[英]MVC Remote validation is adding model to url

I have remote validation on a field and it fires but I get a 500 error saying the parameter is null at the controller method is null. 我在一个字段上进行了远程验证,并且触发了该错误,但是我收到一个500错误,说该参数在控制器方法为null时为null。 What is actually happening is the model name is added to the url, which makes it unable to find the method. 实际发生的是将模型名称添加到url中,这使其无法找到该方法。

Example: 例:

STUDENT model 学生模型

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

For some reason the url created when the validation triggers is: 由于某些原因,验证触发时创建的URL是:

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

Notice the Student. 注意学生。 in the URL. 在网址中。

I have tried a couple different method signatures: 我尝试了几种不同的方法签名:

    public JsonResult CheckExistingResource(string FIrstName, string LastName)

    public JsonResult CheckExistingResource(StudentModel model)

The first one returns the 500 error (null parameter). 第一个返回500错误(空参数)。 The second one passing the Student model gets intot he method, but the first & last are null. 第二个通过Student模型的人进入方法,但是第一个和最后一个为空。

I'm sure this is happening because I am using the Html.BeginCollectionItem helper to build a dynamic list so the user can enter multiple students on the same page. 我确定这是因为我正在使用Html.BeginCollectionItem帮助程序来构建动态列表,以便用户可以在同一页面上输入多个学生。 Part of the BCI helper is that it renames each field with a unique index. BCI帮助程序的一部分是,它使用唯一索引来重命名每个字段。

So I dont think I can stop the url from having model. 因此,我认为我无法阻止URL具有模型。 in it. 在里面。 Is it possible to have model. 有可能有模型。 in the method parameter names? 在方法参数名称中? I tried, it errors. 我试过了,它出错了。

也许模型绑定前缀是按顺序排列的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM