2017年12月14日 星期四

Remote validation (使用RemoteAttribute前端驗證)

https://iamsbc.blogspot.tw/2017/08/mvc-remote-validation-example.html
https://stackoverflow.com/questions/20650955/viewmodel-property-doesnt-bind-into-controller-action-parameter

踩地雷時間:
如果需要兩個參數可以用additionalfields
不過這邊會有一個雷點,這個field只會自動bind到同一個viewmodel內的field
如果要bind其他同一畫面不同viewmodel的話,需要改前端顯示input的
data-val-remote-additionalfields="*.CellPhoneNumber,ApplyTypeViewModel.MobileType"
手動改掉他,如果改不出來甚至不要用內建的html.textboxfor直接寫html input也可以

在@Html.TextBoxFor中new {}修改html attrbute

@data_val_remote_additionalfields = "*.CellPhoneNumber,ApplyTypeViewModel.MobileType"

記得是用底線【_】,轉成HTML後會自己改回【-】


補充資料:後端手動寫入錯誤訊息
https://blog.miniasp.com/post/2016/03/14/ASPNET-MVC-Developer-Note-Part-28-Understanding-ModelState.aspx