简体   繁体   中英

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. What is actually happening is the model name is added to the url, which makes it unable to find the method.

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:

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). The second one passing the Student model gets intot he method, but the first & last are null.

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. Part of the BCI helper is that it renames each field with a unique index.

So I dont think I can stop the url from having model. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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